Skip to content

Instantly share code, notes, and snippets.

@theepicsnail
Last active March 27, 2017 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theepicsnail/8d0ac3dea43a4ac047d6172cac878594 to your computer and use it in GitHub Desktop.
Save theepicsnail/8d0ac3dea43a4ac047d6172cac878594 to your computer and use it in GitHub Desktop.
from collections import defaultdict
def tree():
return defaultdict(tree)
def getAndInsert(value, tree):
# Adds a value to the tree, returns how much of the input collided
collisionLength = 0
cur = tree
for v in value:
if v in cur:
collisionLength += 1
cur = cur[v] # causes the subtree to be written.
return collisionLength
# Example
t = tree()
print getAndInsert("cat", t) # 0
print getAndInsert("car", t) # 2
# uuids are length 36, len(str(uuid.uuid4()))
import uuid
t = tree()
longestCollision = 0
while True:
u = str(uuid.uuid4())
collision = getAndInsert(u, t)
if collision > longestCollision:
longestCollision = collision
print "Longest:", longestCollision
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment