Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. '''
  2. statistics:
  3.  
  4. took 4 days to finish this code.
  5. had issue with debugging and initially made mistake.
  6.  
  7.  
  8. Runnig time.
  9.  
  10. Building the Trie: O(N * K * log N)
  11.  
  12. Look up time: O (K * log N), practically less than O(N)
  13. '''
  14.  
  15. import math
  16.  
  17. class Rank:
  18. def __init__(self, word, rank):
  19. self.word = word
  20. self.rank = rank
  21.  
  22. class Node:
  23. def __init__(self, key):
  24. self.key = key
  25. self.edges = {}
  26. self.children = MaxHeap(3)
  27.  
  28. class MaxHeap:
  29. def __init__(self, capacity):
  30. self.capacity = capacity
  31. self.list = [None]
  32.  
  33. def add(self, node):
  34. self.list.append(node)
  35. self.bubbleUp(len(self.list) - 1)
  36. if len(self.list) > self.capacity + 1:
  37. #remove the last element.
  38. self.list.pop()
  39.  
  40. def extract(self):
  41. if len(self.list) <= 1:
  42. return None
  43.  
  44. value = self.list[1]
  45. self.swap(1, len(self.list) -1)
  46. self.list.pop()
  47. self.bubbleDown(1)
  48. return value
  49.  
  50. def swap(self, l, r):
  51. tmp = self.list[l]
  52. self.list[l] = self.list[r]
  53. self.list[r] = tmp
  54.  
  55. def left(self, p):
  56. return p *2
  57.  
  58. def right(self, p):
  59. return p * 2 + 1
  60.  
  61. def parent(self, c):
  62. return int(math.floor(c / 2))
  63.  
  64. def is_child(self, parent, child):
  65. return self.list[parent].rank <= self.list[child].rank
  66.  
  67. def bubbleUp(self, child):
  68. if child <= 1:
  69. return
  70.  
  71. parent = self.parent(child)
  72.  
  73. if (self.is_child(parent, child) != True):
  74. self.swap(parent, child)
  75. self.bubbleUp(parent)
  76.  
  77. def bubbleDown(self, parent):
  78. if parent >= len(self.list) - 1:
  79. return
  80.  
  81. left = self.left(parent)
  82. right = self.right(parent)
  83.  
  84. if left >= len(self.list):
  85. return
  86.  
  87. to_swap = left if right >= len(self.list) or self.is_child(left, right) == True else right
  88. if self.is_child(parent, to_swap) != True:
  89. self.swap(parent, to_swap)
  90. self.bubbleDown(to_swap)
  91.  
  92. class Trie:
  93. def __init__(self):
  94. self.root = Node('*')
  95.  
  96. def add(self, word, rank):
  97. new_word = Rank(word, rank)
  98. current = self.root
  99.  
  100. for i in new_word.word:
  101. next = current.edges[i] if i in current.edges else None
  102. if next == None:
  103. next = Node(i)
  104. current.edges[i] = next
  105.  
  106. #add new word to next node
  107. next.children.add(new_word)
  108. current = next
  109.  
  110. def find(self, keyword):
  111. found = []
  112. current = self.root
  113. for i in keyword:
  114. if i not in current.edges:
  115. return []
  116. current = current.edges[i]
  117.  
  118. #collect three items from children
  119. for i in range(3):
  120. n = current.children.extract()
  121. if n == None:
  122. break
  123. found.append(n)
  124.  
  125. # small cost to add back items
  126. for n in found:
  127. current.children.add(n)
  128.  
  129. return [[i.word, i.rank] for i in found]
  130.  
  131. t = Trie()
  132. input = [("test", 13), ("tax", 7), ("tom", 50), ("text", 100), ("top", 79),("tub", 101), ("tap", 3)]
  133.  
  134. for i in input:
  135. t.add(i[0], i[1])
  136.  
  137. '2(ABC) 3(DEF) 4(GHI) 5(JKL) 6(MNO) 7(PRQS) 8(TUV) 9(WXYZ)'
  138.  
  139. print("input = {}".format(input))
  140.  
  141. key = 't'
  142. print ("key = {} found = {}".format(key,t.find(key)))
  143.  
  144.  
  145. key = 'ta'
  146. print ("key = {} found = {}".format(key, t.find(key)))
  147.  
  148. key = 'te'
  149. print ("key = {} found = {}".format(key, t.find(key)))
  150.  
  151.  
  152. key = 'to'
  153. print ("key = {} found = {}".format(key, t.find(key)))
  154.  
  155. key = 'a'
  156. print ("key = {} found = {}".format(key, t.find(key)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement