Find word by given characters

Avi Gross avigross at verizon.net
Tue Nov 3 17:14:22 EST 2020


I, too, have wondered what exactly the point was of the required
functionality for SCRABBLE but note you can extend a current word so
additional letters may be available in a game but only if they are an exact
fit to put before, after, or in middle of your word. 

But this seems to be a fairly simple problem to solve unless I misunderstand
it. Elegance aside, what would be wrong with this approach.

- Read a word at a time in a loop from the file of dictionary words
(non-Python meaning of dictionary.) For each one do the following, perhaps
using a function:

Break the current word into a list of individual letters.
Loop over the letters you want and:
	If the letter is in the list, remove it and continue
	Else skip the current word as it is not a match.

At the end of each of the above loops, you only reached here if all the
letters were found and removed. If the list is now empty, fine. If it has
extra remaining letters, also fine by the requirements stated. Letters in
the list multiple times are removed multiple times.

The above need not use  list of letters and can be done many other ways but
seems conceptually simple. Each word is processed once. It can be converted
to using a list comprehension or something similar by using "all" and so on.

Or am I missing something about other approaches being better or more
efficient or ... And, yes, counting may have an advantage as the list does
not need to be modified repeatedly but creating an object or three also has
overhead.

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On
Behalf Of Chris Angelico
Sent: Tuesday, November 3, 2020 12:35 PM
To: Python <python-list at python.org>
Subject: Re: Find word by given characters

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
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list