Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find all words in a certain alphabet with multi character letters

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.

like image 308
Nivatius Avatar asked Mar 05 '23 15:03

Nivatius


2 Answers

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 expression

Alltogether this "reads" as "whole string is one or more repetition of fis/cis/dis or one of abcfhg"

like image 110
Slam Avatar answered Mar 08 '23 18:03

Slam


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.


Update:

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.

like image 20
Benoît P Avatar answered Mar 08 '23 19:03

Benoît P