Find word by given characters

dn PythonList at DancesWithMice.info
Mon Nov 2 14:09:24 EST 2020


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"

This process assumes that built-in functions are faster than exhaustive 
scans written in Python, and thus (presumably) also that the number of 
"Letters" is small in comparison with the lengths of words.


Once the algorithms are proven, a speed comparison might be an 
interesting exercise...

For extra credit: once you've solved both, and compared the alternatives 
on your machine; post the code (and test data), and ask various 
colleagues 'here' to repeat the speed/performance comparisons on other 
machines.

Will/should the results be identical?
-- 
Regards =dn


More information about the Python-list mailing list