Find word by given characters

Chris Angelico rosuav at gmail.com
Tue Nov 3 12:34:51 EST 2020


On Wed, Nov 4, 2020 at 1:11 AM Bischoop <Bischoop at vimart.net> wrote:
> 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.
> I've checked collections counter but seems to complicated for me at this
> stage yet and I'm struggling with applying it.

This seems strangely backwards for a Scrabble game. Normally you would
have a set of available tiles, and you have to form a word using only
those tiles, but it doesn't necessarily have to use them all. You seem
to have something where you must use all the tiles you have, and may
use any number of others. But, no matter; it can be done either way.

>>> from collections import Counter
>>> Counter("att") <= Counter("toast")
True
>>> Counter("att") <= Counter("toasty")
True
>>> Counter("att") <= Counter("tolerant")
True
>>> Counter("att") <= Counter("auto")
False

A Counter can behave like a multiset. If you picture the <= operator
as being "is a subset of", and then squint a little, irritate your
math teacher, and pretend that a set can have more than one of the
same thing in it, then a Counter's less-than-or-equal operator will
tell you if one multiset is a subset of another.

(The normal way to play Scrabble would simply reverse the operands, so
you'd ask if the word is <= the tiles you have. It's otherwise exactly
the same check.)

ChrisA


More information about the Python-list mailing list