Advertisement
bwukki

week 8 data structures

Nov 25th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | None | 0 0
  1.  
  2. #question 1
  3. def sanatizeinput(user_exp): #allows function to accept expressions with or without spaces
  4.     user_exp = list(user_exp)
  5.     clean_exp = []
  6.     operators = ["(",'+', '-', '*', '/', ')']
  7.     char = 0
  8.     print(user_exp)
  9.     while True:
  10.         if char >= len(user_exp) or len(user_exp) == 1:
  11.             clean_exp.extend(user_exp)
  12.             break
  13.         if user_exp[char] in operators:
  14.             result = ''
  15.             for i in user_exp[:char]:
  16.                 result += i
  17.             clean_exp.append(result)
  18.             clean_exp.append(user_exp[char])
  19.             user_exp = user_exp[char+1:]
  20.             char = -1
  21.         char +=1
  22.     return clean_exp
  23.  
  24. #question 5
  25.    def put(self, key, val): #replaces node instead of making a a new one
  26.        if self.root and self.key != key:
  27.            self._put(key, val, self.root)
  28.        else:
  29.            self.root = TreeNode(key, val)
  30.        self.size = self.size + 1
  31.  
  32. #question 6 and 10, entire thing from scratch so it might suck
  33.  
  34. import time
  35.  
  36.  
  37. class BinaryHeap:
  38.     def __init__(self, size, data):
  39.         self.size = size
  40.         self.content = [None]
  41.         if len(data) <= self.size:
  42.             for i in data:
  43.                 self.content.append(Node(i))
  44.             self.max_heap()
  45.         else:
  46.             for i in range(size):
  47.                 self.content.append(Node(data[i]))
  48.             for i in range(size, len(data)):
  49.                 self.add(data[i])
  50.  
  51.     def add(self, input):
  52.         if len(self.content) <= self.size:
  53.             self.content.append(Node(input))
  54.         else:
  55.             self.min_heap()
  56.             if self.content[1].value < input:
  57.                 self.content[1] = Node(input)
  58.         self.max_heap()
  59.  
  60.     def min_heap(self):
  61.         heaps_to_do = True
  62.         while heaps_to_do:
  63.             heaps_to_do = False
  64.             current_pos = len(self.content) // 2
  65.             going_through_list = True
  66.             while going_through_list:
  67.                 try:
  68.                     if self.content[current_pos].value > self.content[current_pos*2+1].value:
  69.                         self.content[current_pos], self.content[current_pos*2+1] = self.content[current_pos*2+1], self.content[current_pos]
  70.                         heaps_to_do = True
  71.                 except IndexError:
  72.                     pass
  73.                 if self.content[current_pos].value > self.content[current_pos*2].value:
  74.                     self.content[current_pos], self.content[current_pos*2] = self.content[current_pos*2], self.content[current_pos]
  75.                     heaps_to_do = True
  76.                 if current_pos <= 1:
  77.                     going_through_list = False
  78.                 current_pos += -1
  79.  
  80.     def max_heap(self):
  81.         heaps_to_do = True
  82.         while heaps_to_do:
  83.             heaps_to_do = False
  84.             current_pos = (len(self.content)-1) // 2
  85.             going_through_list = True
  86.             while going_through_list:
  87.                 try:
  88.                     if self.content[current_pos].value < self.content[current_pos*2+1].value:
  89.                         self.content[current_pos], self.content[current_pos*2+1] = self.content[current_pos*2+1], self.content[current_pos]
  90.                         heaps_to_do = True
  91.                 except IndexError:
  92.                     pass
  93.                 if self.content[current_pos].value < self.content[current_pos*2].value:
  94.                     self.content[current_pos], self.content[current_pos*2] = self.content[current_pos*2], self.content[current_pos]
  95.                     heaps_to_do = True
  96.                 if current_pos <= 1:
  97.                     going_through_list = False
  98.                 current_pos += -1
  99.  
  100.     def __str__(self):
  101.         result = ""
  102.         for i in self.content:
  103.             if i is not None:
  104.                 result += str(i) + " "
  105.         return result
  106.  
  107.  
  108. class Node:
  109.     def __init__(self, value):
  110.         self.value = value
  111.  
  112.     def __str__(self):
  113.         return str(self.value)
  114.  
  115.  
  116. x = [i for i in range(100000)]
  117. start_time = time.time()
  118. y = BinaryHeap(100000, x)
  119. print(time.time()-start_time)
  120. print(y)
  121. start_time = time.time()
  122. y.add(1000000000000)
  123. print(time.time()-start_time)
  124. print(y)
  125.  
  126.  
  127. #8
  128. result = []
  129. for i in range(self.size):
  130.     result.append(self.content.pop(1).value)
  131.     self.max_heap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement