Find word by given characters

duncan smith duncan at invalid.invalid
Tue Nov 3 20:34:43 EST 2020


On 03/11/2020 23:35, Bischoop wrote:
> On 2020-11-03, duncan smith <duncan at invalid.invalid> wrote:
>>>
>>
>>>>> from collections import Counter
>>>>> letters = 'att'
>>>>> letter_counts = Counter(letters)
>>>>> word = 'tolerate'
>>>>> wd_counts = Counter(word)
>>>>> for char, cnt in letter_counts.items():
>> 	print (cnt == wd_counts[char])
>>
>> 	
>> True
>> True
>>>>> word = 'auto'
>>>>> wd_counts = Counter(word)
>>>>> for char, cnt in letter_counts.items():
>> 	print (cnt == wd_counts[char])
>>
>> 	
>> True
>> False
>>>>>
>>
>> or, equivalent to the above loop, but breaking when the first False is
>> generated and returning the single Boolean that you're after,
>>
>>>>> all(cnt == wd_counts[char] for char, cnt in letter_counts.items())
>> False
>>>>>
>>
>> There's still a lot of scope for improvement, but possibly not by doing
>> simple things
> 
> 
> lol
> 
> I'm thinking about it for a couple days and you guys coming with few
> ideas to it like it's nothing.
> Pity I've wasted a decade with woman, now probably to old to learn
> anything new.
> 

It looks like you've learnt something about wasting time with women ;-).
Keep tinkering as long as you're enjoying it (or getting paid for it)
and pick up knowledge of relevant data structures and algorithms as you
go. (That's what I've done. I'm actually a statistician.) The above
solution uses bags (implemented in Python as Counter instances), and the
(repeated) generation of the letters bag was hoisted outside the for
loop. (If the dictionary was going to be searched for multiple sets of
letters the generation of the word bags would also be hoisted.) There's
also the idea of breaking out of the loop once the first False is
generated (implemented most neatly by using all). So there might be
something useful to take from it.

A bag was the obvious data structure because the solution depends only
on the unique characters in a string and their frequencies. But that's
thinking about the problem word by word. We actually have a dictionary
of words, and a dictionary can be structured so that it can be searched
much more efficiently than 'word by word'. The particular data structure
I have in mind is not (yet) in the standard Python library. That's maybe
worth looking at when you've got the word by word approach grokked.

Duncan


More information about the Python-list mailing list