Does '#hash' mean anything in IDLE?

Paul Rubin http
Fri Mar 3 15:43:56 EST 2006


Scott David Daniels <scott.daniels at acm.org> writes:
> And, for 2.4 or later:
> 
>      def letter_hash(word):
>          return "".join(sorted(word))
> 
> sorted takes an iterable, and strings are iterables.

I don't think the "hash" is really a hash in the normal sense--in
particular, it has to be collision-free.  So I'd just call it
"sorted_word".  Here's my version of the program:

================================================================
from sets import Set

def sorted_word(word):
    return ''.join(sorted(word))

d = {}
for line in file('/usr/share/dict/words'):
    word = line.strip().lower()
    d.setdefault(sorted_word(word), Set()).add(word)

print sorted(d.iteritems(), key=lambda (x,y): -len(y))[:1]

================================================================

For the version of /usr/dict/words with Fedora Core 4, I get the words
('reast', 'stare', 'arest', 'tares', 'resat', 'aster', 'treas',
'teras', 'tears', 'rates', 'serta', 'tarse', 'astre', 'strae',
'tresa').  I don't know what most of those words mean.

Note that I sorted the dictionary items in order to get the max
element.  That is sort of bogus because it's an O(N log N) operation
while finding the maximum should only need O(N).  But it leads to
a convenient spelling.  It would be nice if "max" accepted a "key"
argument the way that the sorting functions do.

I used sets.Set instead of a list in order to prevent ABC and abc
(identical after processing through .lower()) from counting as
anagrams of each other.



More information about the Python-list mailing list