Find word by given characters

duncan smith duncan at invalid.invalid
Mon Nov 2 19:13:10 EST 2020


On 02/11/2020 19:09, dn wrote:
> On 02/11/2020 23:29, Bischoop wrote:
>> On 2020-11-01, duncan smith <duncan at invalid.invalid> wrote:
>>>>
>>>
>>> But this generates the letters counts for each word. They only need to
>>> be generated once (outside the for loop). And using count requires
>>> iterating over the letters / words for each x in letters (rather than
>>> once). For a large enough collection of words you'd notice the
>>> difference.
>>>
>>
>> You're pretty much right, it doesn't go too fast.
>> I've done this years ago with Python2, had a bit free time now and
>> wanted to ReLearn Python from starting old projects I've done in past
>> but can't remember how I made it working lol.
> 
> 
> If you have a working Py2 version, once print-statements were changed
> into functions, what errors were thrown-up?
> 
> 
> Multiple loops written in Python are likely to be slower than same in
> compiled code - which was probably part of the motivation for @Terry's
> response. Plus, "re-use" - why write something ourselves if someone else
> has already done the work?
> 
> 
> How about a change of tactics?
> 
> - str.find() or .index() will locate a character within the string
> (starting from character[0]/the left-hand side)
> - if this fails, tears will fall...
> - repeat, from the right
> - if both results are the same character/position, it must be unique
> within the string
> - repeat for each character in "Letters"
> 

[snip]

But he appears to need the count in order to compare against the counts
in letters. I assume letters could be something like 'att', in which
case knowing a word contains more than one 't' doesn't cut it. He could
try iterating over the characters in each word once, checking that no
count from letters is exceeded, and that the number of remaining
characters gives some chance that the relevant counts from letters could
be achieved. In the worst case (e.g. a conforming word) this requires
iterating over all the characters in a word once. That's not to say it
would actually run quicker 'on average' than a solution using Counter.

Duncan


More information about the Python-list mailing list