Best Python code snippet using autotest_python
priorityQueue.py
Source:priorityQueue.py
1class Node:2 def __init__(self, value, priority):3 self.value = value4 self.priority = priority5 def get_priority(self):6 return self.priority7 def get_value(self):8 return self.value9# we will be using min heap for this10class PriorityQueue:11 def __init__(self):12 self.values = []13 def enqueue(self, value, priority):14 new_node = Node(value, priority)15 self.insert(new_node)16 def insert(self, node):17 if len(self.values) <= 0:18 self.values.append(node)19 return20 self.values.append(node)21 index = len(self.values) - 122 parent_index = (index - 1) // 223 while self.values[index].get_priority() < self.values[parent_index].get_priority() and parent_index >= 0:24 self.values[parent_index], self.values[index] = self.values[index], self.values[parent_index]25 index = parent_index26 parent_index = (index - 1) // 227 def dequeue(self):28 return self.extract_min()29 def extract_min(self):30 if len(self.values) <= 0:31 return None32 if len(self.values) == 1:33 return self.values.pop()34 length = len(self.values) - 135 self.values[0], self.values[length] = self.values[length], self.values[0]36 return_node = self.values.pop()37 index = 038 while True:39 left_child = (2*index) + 140 right_child = (2 * index) + 241 if left_child < 0 or left_child >= length:42 return return_node43 if right_child < 0 or right_child >= length:44 return return_node45 # \ is for simplified operation, in python in expression if we wanna go to new line, we use \46 if self.values[index].get_priority() < self.values[left_child].get_priority() and self.values[index].get_priority() < self.values[right_child].get_priority():47 return return_node48 if self.values[index].get_priority() > self.values[right_child].get_priority() > self.values[left_child].get_priority():49 self.values[index], self.values[left_child] = self.values[left_child], self.values[index]50 index = left_child51 continue52 if self.values[index].get_priority() > self.values[left_child].get_priority() > self.values[right_child].get_priority():53 self.values[index], self.values[right_child] = self.values[right_child], self.values[index]54 index = right_child55 continue56 if self.values[index].get_priority() > self.values[left_child].get_priority():57 self.values[index], self.values[left_child] = self.values[left_child], self.values[index]58 index = left_child59 continue60 if self.values[index].get_priority() > self.values[right_child].get_priority():61 self.values[index], self.values[right_child] = self.values[right_child], self.values[index]62 index = right_child63 continue64 def returnQueue(self):65 return_heap = []66 for i in range(len(self.values)):67 value_priority_pair = {self.values[i].get_priority(): self.values[i].get_value()}68 return_heap.append(value_priority_pair)...
a1_test.py
Source:a1_test.py
...12queue.insert('H', 20)13queue.insert('U', -5)14queue.insert('X', -20)15queue.changepriority(3, 5)16print(queue.getoddlist().get_priority())17print(queue.getevenlist().get_item())18print(queue.getoddlist().get_next().get_priority())19print(queue.getevenlist().get_next().get_priority())20print(queue.getoddlist().get_next().get_next().get_priority())21print(queue.getevenlist().get_next().get_next().get_priority())22print(queue.getoddlist().get_next().get_next().get_next().get_priority())23print(queue.getevenlist().get_next().get_next().get_next().get_priority())24print(queue.getoddlist().get_next().get_next().get_next().get_next().get_priority())
...
a1_test2.py
Source:a1_test2.py
...13queue.insert('G', 0)14queue.insert('H', -88)15queue.insert('H', 99)1617print(queue.getoddlist().get_priority())18print(queue.getevenlist().get_priority())19print(queue.getoddlist().get_next().get_priority())20print(queue.getevenlist().get_next().get_priority())21print(queue.getoddlist().get_next().get_next().get_priority())22print(queue.getevenlist().get_next().get_next().get_priority())23print(queue.getoddlist().get_next().get_next().get_next().get_priority())24print(queue.getevenlist().get_next().get_next().get_next().get_priority())
...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!