Does '#hash' mean anything in IDLE?

Blackbird fake at nospam.no
Sat Mar 4 02:36:27 CET 2006


Scott David Daniels wrote:
> Paul Rubin wrote:
>> 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
> "Cute" form for this:
>
>      try:
>          set
>      except NameError:
>          from sets import Set as set
>
> Then you get native sets for 2.4, and sets.Set for 2.3
>
>> 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]
> ...
>
>> 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.
>
> Using a variant of DSU (Decorate-Sort-Undecorate) with max for S,
> rather than sort:
>
>      print max((len(words), words) for words in d.itervalues())
> or:
>      size, words = max((len(words), words) for words in
>      d.itervalues()) print size, sorted(words)
>
>
> --Scott David Daniels
> scott.daniels at acm.org

Your code is Pylegant.  Jee, I just learned of Python two weeks ago, and my
copy of the cookbook arrived yesterday.  And now I'm coining new words.
What is this language doing to me?

Blackbird





More information about the Python-list mailing list