Paul Rubin wrote:
> Scott David Daniels <[EMAIL PROTECTED]> 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
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list