I want to find out what words can be formed using the names of musical notes.
This question is very similar: Python code that will find words made out of specific letters. Any subset of the letters could be used But my alphabet also contains "fis","cis" and so on.
letters = ["c","d","e","f","g","a","h","c","fis","cis","dis"]
I have a really long word list with one word per list and want to use
with open(...) as f:
for line in f:
if
to check if each word is part of that "language" and then save it to another file.
my problem is how to alter
>>> import re
>>> m = re.compile('^[abilrstu]+$')
>>> m.match('australia') is not None
True
>>> m.match('dummy') is not None
False
>>> m.match('australian') is not None
False
so it also matches with "fis","cis" and so on.
e.g. "fish" is a match but "ifsh" is not a match.
I believe ^(fis|cis|dis|[abcfhg])+$
will do the job.
Some deconstruction of what's going on here:
|
workds like OR conjunction[...]
denotes "any symbol from what's inside the brackets"^
and $
stand for beginning and end of line, respectively+
stands for "1 or more time"( ... )
stands for grouping, needed to apply +
/*
/{}
modifiers. Without grouping such modifiers applies to closest left expressionAlltogether this "reads" as "whole string is one or more repetition of fis/cis/dis or one of abcfhg"
This function works, it doesn't use any external libraries:
def func(word, letters):
for l in sorted(letters, key=lambda x: x.length, reverse=True):
word = word.replace(l, "")
return not s
it works because if s==""
, then it has been decomposed into your letters.
It seems that my explanation wasn't clear. WORD.replace(LETTER, "")
will replace the note/LETTER in WORD by nothing, here is an example :
func("banana", {'na'})
it will replace every 'na'
in "banana"
by nothing (''
)
the result after this is "ba"
, which is not a note
not ""
means True
and not "ba"
is false, this is syntactic sugar.
here is another example :
func("banana", {'na', 'chicken', 'b', 'ba'})
it will replace every 'chicken'
in "banana"
by nothing (''
)
the result after this is "banana"
it will replace every 'ba'
in "banana"
by nothing (''
)
the result after this is "nana"
it will replace every 'na'
in "nana"
by nothing (''
)
the result after this is ""
it will replace every 'b'
in ""
by nothing (''
)
the result after this is ""
not ""
is True
==> HURRAY IT IS A MELODY !
note: The reason for the sorted
by length is because otherwise, the second example would not have worked. The result after deleting "b" would be "a", which can't be decomposed in notes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With