Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #question 1
- def sanatizeinput(user_exp): #allows function to accept expressions with or without spaces
- user_exp = list(user_exp)
- clean_exp = []
- operators = ["(",'+', '-', '*', '/', ')']
- char = 0
- print(user_exp)
- while True:
- if char >= len(user_exp) or len(user_exp) == 1:
- clean_exp.extend(user_exp)
- break
- if user_exp[char] in operators:
- result = ''
- for i in user_exp[:char]:
- result += i
- clean_exp.append(result)
- clean_exp.append(user_exp[char])
- user_exp = user_exp[char+1:]
- char = -1
- char +=1
- return clean_exp
- #question 5
- def put(self, key, val): #replaces node instead of making a a new one
- if self.root and self.key != key:
- self._put(key, val, self.root)
- else:
- self.root = TreeNode(key, val)
- self.size = self.size + 1
- #question 6 and 10, entire thing from scratch so it might suck
- import time
- class BinaryHeap:
- def __init__(self, size, data):
- self.size = size
- self.content = [None]
- if len(data) <= self.size:
- for i in data:
- self.content.append(Node(i))
- self.max_heap()
- else:
- for i in range(size):
- self.content.append(Node(data[i]))
- for i in range(size, len(data)):
- self.add(data[i])
- def add(self, input):
- if len(self.content) <= self.size:
- self.content.append(Node(input))
- else:
- self.min_heap()
- if self.content[1].value < input:
- self.content[1] = Node(input)
- self.max_heap()
- def min_heap(self):
- heaps_to_do = True
- while heaps_to_do:
- heaps_to_do = False
- current_pos = len(self.content) // 2
- going_through_list = True
- while going_through_list:
- try:
- if self.content[current_pos].value > self.content[current_pos*2+1].value:
- self.content[current_pos], self.content[current_pos*2+1] = self.content[current_pos*2+1], self.content[current_pos]
- heaps_to_do = True
- except IndexError:
- pass
- if self.content[current_pos].value > self.content[current_pos*2].value:
- self.content[current_pos], self.content[current_pos*2] = self.content[current_pos*2], self.content[current_pos]
- heaps_to_do = True
- if current_pos <= 1:
- going_through_list = False
- current_pos += -1
- def max_heap(self):
- heaps_to_do = True
- while heaps_to_do:
- heaps_to_do = False
- current_pos = (len(self.content)-1) // 2
- going_through_list = True
- while going_through_list:
- try:
- if self.content[current_pos].value < self.content[current_pos*2+1].value:
- self.content[current_pos], self.content[current_pos*2+1] = self.content[current_pos*2+1], self.content[current_pos]
- heaps_to_do = True
- except IndexError:
- pass
- if self.content[current_pos].value < self.content[current_pos*2].value:
- self.content[current_pos], self.content[current_pos*2] = self.content[current_pos*2], self.content[current_pos]
- heaps_to_do = True
- if current_pos <= 1:
- going_through_list = False
- current_pos += -1
- def __str__(self):
- result = ""
- for i in self.content:
- if i is not None:
- result += str(i) + " "
- return result
- class Node:
- def __init__(self, value):
- self.value = value
- def __str__(self):
- return str(self.value)
- x = [i for i in range(100000)]
- start_time = time.time()
- y = BinaryHeap(100000, x)
- print(time.time()-start_time)
- print(y)
- start_time = time.time()
- y.add(1000000000000)
- print(time.time()-start_time)
- print(y)
- #8
- result = []
- for i in range(self.size):
- result.append(self.content.pop(1).value)
- self.max_heap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement