word shifts

Arnaud Delobelle arnodel at googlemail.com
Sun May 4 07:56:48 EDT 2008


bearophileHUGS at lycos.com writes:

> George Sakkis:
>> A faster algorithm is to create a 'key' for each word, defined as the
>> tuple of ord differences (modulo 26) of consecutive characters.
>
> Very nice solution, it uses the same strategy used to find anagrams,
> where keys are
> "".join(sorted(word))
> Such general strategy to look for a possible invariant key for the
> subsets of the required solutions is quite useful.

Or even:

def get_key(word):
    initial = ord(word[0])
    return tuple((ord(x) - initial) % 26 for x in word[1:])

-- 
Arnaud



More information about the Python-list mailing list