Skip to content

Instantly share code, notes, and snippets.

@shklqm
Last active March 2, 2016 19:28
Show Gist options
  • Save shklqm/ec151ef229704bbe26d0 to your computer and use it in GitHub Desktop.
Save shklqm/ec151ef229704bbe26d0 to your computer and use it in GitHub Desktop.
#!/bin/python
from random import shuffle
def insert(n1,n2,tree):
if n2 in tree:
print "inserted1 with key", n2
d1 = {n1:{}}
tree[n2] = dict(tree[n2].items() + d1.items())
# print "returning...1"
return True
elif n1 in tree:
print "inserted2 with key",n1
d1 = {n2:{}}
tree[n1] = dict(tree[n1].items() + d1.items())
# print "returning...2"
return True
else:
for key,val in tree.items():
if insert(n1,n2,val):
return True
# print "returning...END"
return False
count = 0
tree = {}
treeTuples = []
def countValues(tree):
global count
for key,val in tree.items():
count += 1
countValues(val)
return count
t = int(raw_input().strip())
for a0 in xrange(t):
n1, n2 = raw_input().strip().split(' ')
treeTuples.append((n1,n2))
# shuffle(treeTuples)
it = 0
for t in treeTuples:
n1 = t[0]
n2 = t[1]
it+=1
if not (insert(n1,n2,tree)):
d1 = {n1:{}}
d2 = {n2:d1}
print "NOT found at: ",it," ---> ", n1,n2
tree = dict(tree.items() + d2.items())
print "tree == ",tree
else:
print "FOUND at: ", it
print "tree == ",tree
# print treeTuples
# print "-----------------------"
# print countValues(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment