def test_queues(self): q = Queue() for i in range(1,100): q.push(i) self.assertEqual(99,q.size()) self.assertEqual(1,q.peek()) self.assertEqual(1,q.pop())
class RPN_converter: """convert a list of numbers and operations and functions to RPN format""" def __init__(self): self.operators = Stack() self.output = Queue() self.out = [] def convert(self, elements): """convert given list to RPN format""" for element in elements: if isinstance(element, (float, int)): self.output.push(element) elif isinstance(element, Function): self.operators.push(element) elif element == "(": self.operators.push(element) elif element == ")": while self.operators.peek() != "(": self.output.push(self.operators.pop()) self.operators.pop() elif isinstance(element, Operator): while self.operators.peek() != None or self.operators.peek( ) != "(": if isinstance(self.operators.peek(), Operator) and \ self.operators.peek() > element: self.output.push(self.operators.pop()) else: break self.operators.push(element) while self.operators.peek() != None: self.output.push(self.operators.pop()) return self.output.items