Find word by given characters
Ben Bacarisse
ben.usenet at bsb.me.uk
Tue Nov 3 09:41:39 EST 2020
Bischoop <Bischoop at vimart.net> writes:
> Let me clarify what I want to do:
> We all know Scrabble game.
> there's a file with Dictionary containing word in each line, the idea is
> to input some letters for example mentioned earlier: att, the script
> supposed to find all words in which letters: 'a','t','t' occur and print
> these words. It supposed not to print word: 'auto' because there's only
> one 't' but words such as: 'toast', 'toasty', 'tolerant' are meeting the
> criteria becase we have in these words one 'a' and two 't' as user has
> input.
In Scrabble, you will usually know the order you want for the known
letters, so you could just make a regular expression: t.*a.*t
In fact, you often know the gaps, so you might even be able to match
something more specific like t.a..t instead.
If you don't know the order, you can still make a chain of matches. For
example, matching for t.*t and then also a. Every distinct letter needs
a match, and repeated letters need a single match with .* between them.
And since Python REs have (?=...) look ahead syntax you could even make
a single pattern like this:
(?=.*t.*t)(?=.*a)
While there are probably better ways in Python, this is what I'd do on
the command line. For example
$ grep -P '(?=.*t.*t)(?=.*a)' word-list | wc -l
45677
$ grep 't.*t' word-list | grep a | wc -l
45677
--
Ben.
More information about the Python-list
mailing list