bertkellerman
BAN USERimport bst
import random
def longest_path_length_bst(root):
if root is None:
return 0
return max(longest_path_length_bst(root.left), longest_path_length_bst(root.right)) + 1
def longest_path_bst(root):
if root is None:
return []
left_path = longest_path_bst(root.left)
right_path = longest_path_bst(root.right)
if len(right_path) > len(left_path):
max_path = right_path
else:
max_path = left_path
max_path.append(root.data)
return max_path
tree = bst.BST()
data = [random.randint(0, 1000000) for x in range(1000)]
tree.insert(data)
print longest_path_bst(tree.root)
def get_counter(s):
d = {}
for c in list(s):
if c in d:
d[c] += 1
else:
d[c] = 1
return d
def get_agram_idx(s, h):
if len(s) > len(h):
return None
if s is None or h is None:
return None
cc = get_counter(s)
agram_idx = []
agram_found = False
for i in range(len(h) +1 - len(s)):
#short circuit logic, just check if new char is same as old
if agram_found:
if h[i-1] == h[i+len(s)-1]:
agram_idx.append(i)
else:
agram_found = False
continue
c = cc.copy()
for j in range(len(s)):
if h[i+j] not in c:
break
c[h[i+j]] -= 1
if set(c.values()) == set([0]):
agram_idx.append(i)
return agram_idx
- bertkellerman January 22, 2017