thraxil.org:

Reply to: A Simple Programming Puzzle Seen Through Three Different Lenses

Great post!

I had heard about the `groupby’ function, but never about `clusterby’ (which seems much more useful). You r first solution that you posted look pretty much how I would had done it. Although, I have to admit that Tom Moertel’s solution is quite elegant.

Anyway, I improved slightly your translation of Moertel’s solution. I think you will appreciate the few tweaks I did:

from collections import defaultdict
def sort(x):
  return "".join(sorted(x))
def clusterby(iterable, key):
  d = defaultdict(list)
  for x in iterable:
    d[key(x)].append(x)
  return d.values()
c = clusterby([x+y for x in states for y in states], sort)
print max(c, key=len)

P.S.: Your anti-spam challenge is ambiguous.


formatting is with Textile syntax. Comments are not displayed until they are approved by a moderator. Moderators will not approve unless the comment contributes value to the discussion.

namerequired
emailrequired
url
remember info?