Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. import os
  2.  
  3. class PoliceNode:
  4. """docstring for PoliceNode"""
  5. def __init__(self, policeId, fineAmt):
  6.  
  7. self.policeId = policeId
  8. self.fineAmt = fineAmt
  9. self.left = None
  10. self.right = None
  11.  
  12.  
  13. class DriverHash:
  14. """docstring for DriverHash"""
  15. def __init__(self, max_size):
  16. self._map = None
  17. self.max_size = max_size
  18.  
  19. def initializeHash(self):
  20. self._map = [None for i in range(0, self.max_size)]
  21.  
  22. def get(self, hash):
  23. return self._map[hash]
  24.  
  25. def put(self, hash_index, value):
  26. self._map[hash_index] = value
  27.  
  28.  
  29. def insertHash(driverhash, lic):
  30. """
  31. Inserts/increments the counter for given licence number
  32. Note: considering licence number as Integer
  33. """
  34. hash = lic%driverhash.max_size
  35. bucket = driverhash.get(hash)
  36. if bucket is not None:
  37. hasLicence=False
  38. for index, (licence, violation_cnt) in enumerate(bucket):
  39. if licence==lic:
  40. hasLicence = True
  41. bucket[index] = (lic, violation_cnt+1)
  42. if not hasLicence:
  43. bucket.append((lic, 1))
  44. else:
  45. driverhash.put(hash,[(lic, 1)])
  46.  
  47.  
  48. def printViolators(driverhash):
  49.  
  50. with open("violators.txt", "w") as f:
  51. for hash_index in range(0, driverhash.max_size):
  52. if driverhash.get(hash_index):
  53. for licence, violation_cnt in driverhash.get(hash_index):
  54.  
  55. if violation_cnt>3:
  56. # given more than 3 in qn
  57. f.write("{},{}\n".format(licence, violation_cnt))
  58.  
  59.  
  60. def destroyHash(driverhash):
  61. driverhash.hashmap = None
  62.  
  63. def insertByPoliceId(policeRoot, policeId, amount):
  64.  
  65. if policeRoot is None:
  66. policeRoot = PoliceNode(policeId, amount)
  67. return policeRoot
  68. elif policeId == policeRoot.policeId:
  69. policeRoot.fineAmt = policeRoot.fineAmt + amount
  70. return policeRoot
  71. elif policeId<policeRoot.policeId:
  72. policeRoot.left = insertByPoliceId(policeRoot.left, policeId, amount)
  73. return policeRoot
  74. else:
  75. policeRoot.right = insertByPoliceId(policeRoot.right, policeId, amount)
  76. return policeRoot
  77.  
  78. def insertByAmount(root, current):
  79.  
  80. if root is None:
  81. root = current
  82. return root
  83. elif current.fineAmt>root.fineAmt:
  84. root.right = insertByAmount(root.right, current)
  85. return root
  86. else:
  87. root.left = insertByAmount(root.left, current)
  88. return root
  89.  
  90.  
  91. def reorderByFineAmount(policeRoot):
  92. if policeRoot is None:
  93. return
  94. queue = []
  95. newRoot = None
  96. queue.append(policeRoot)
  97. while(len(queue) > 0):
  98. node = queue.pop(0)
  99. print(node.policeId)
  100.  
  101. if node.left is not None :
  102. queue.append(node.left)
  103. if node.right is not None:
  104. queue.append(node.right)
  105. node.left=None
  106. node.right=None
  107. newRoot = insertByAmount(newRoot, node)
  108. return newRoot
  109.  
  110.  
  111. def max_fine_amount(policeRoot):
  112. if policeRoot is not None:
  113. max_fine_amt = policeRoot.fineAmt
  114. lmax = max_fine_amount(policeRoot.left) or 0
  115. rmax = max_fine_amount(policeRoot.right) or 0
  116. if (lmax>max_fine_amt):
  117. max_fine_amt = lmax
  118. if (rmax>max_fine_amt):
  119. max_fine_amt = rmax
  120. return max_fine_amt
  121.  
  122. def printBonusPolicemen(policeRoot):
  123. if policeRoot is not None:
  124. print(policeRoot.fineAmt)
  125. max_fine_90p = max_fine_amount(policeRoot) * 0.9
  126. queue = []
  127. queue.append(policeRoot)
  128. with open("bonus.txt", "w") as f:
  129. while(len(queue) > 0):
  130.  
  131. node = queue.pop(0)
  132. if node.fineAmt>=max_fine_90p:
  133. f.write("{},{}\n".format(node.policeId, node.fineAmt))
  134. if node.left is not None :
  135. queue.append(node.left)
  136. if node.right is not None:
  137. queue.append(node.right)
  138.  
  139. def printPoliceTree(policeRoot):
  140.  
  141. if policeRoot is not None:
  142. printPoliceTree(policeRoot.left)
  143. print("{},{}".format(policeRoot.policeId, policeRoot.fineAmt))
  144. printPoliceTree(policeRoot.right)
  145.  
  146.  
  147. def destroyPoliceTree (policeRoot):
  148. policeRoot=None
  149. if __name__ == '__main__':
  150. import sys
  151. driverhash= DriverHash(16)
  152. driverhash.initializeHash()
  153. policeRoot = None
  154. with open("inputPS3.txt","r") as fp:
  155. for line in fp :
  156. try:
  157. if line.strip()!="":
  158. police_id = int(line.split("/")[0])
  159. licence = int(line.split("/")[1])
  160. amount = int(line.split("/")[2])
  161. insertHash(driverhash, licence)
  162. policeRoot = insertByPoliceId(policeRoot, police_id, amount)
  163. except Exception as e:
  164. print(e)
  165.  
  166. printViolators(driverhash)
  167. printPoliceTree(policeRoot)
  168. newPoliceRoot = reorderByFineAmount(policeRoot)
  169. printPoliceTree(newPoliceRoot)
  170. printBonusPolicemen(newPoliceRoot)
  171. destroyHash(driverhash)
  172. destroyPoliceTree(policeRoot)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement