Last active
March 27, 2017 19:53
-
-
Save theepicsnail/8d0ac3dea43a4ac047d6172cac878594 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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