Skip to content

Instantly share code, notes, and snippets.

@bmispelon
Created February 22, 2013 08:08
Show Gist options
  • Select an option

  • Save bmispelon/5011667 to your computer and use it in GitHub Desktop.

Select an option

Save bmispelon/5011667 to your computer and use it in GitHub Desktop.
Looking for dict collisions
import itertools
from string import ascii_letters
import sys
def collisions(alphabet=ascii_letters, n=6):
d = {}
for i in range(2, n):
for comb in itertools.combinations_with_replacement(alphabet, i):
w = ''.join(comb)
h = hash(w)
if h in d:
return d[h], w
d[h] = w
if __name__ == "__main__":
if len(sys.argv) > 1:
n = int(sys.argv[1])
else:
n = 6
collision = collisions(n=n)
if collision:
print 'Collision found: %s, %s' % collision
else:
print 'No collision found :('
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment