def __init__(self, exp): # DST = Data Structure self.expressionOperands = LinkedListStack() self.expressionOperators = LinkedListStack() self.expected_operators = {'÷': '/', 'x': '*', '+': '+', '-': '-', '(': '(', ')': ')'} self.exp = exp
class Expression: def __init__(self, exp): # DST = Data Structure self.expressionOperands = LinkedListStack() self.expressionOperators = LinkedListStack() self.expected_operators = {'÷': '/', 'x': '*', '+': '+', '-': '-', '(': '(', ')': ')'} self.exp = exp def evaluate(self): parsed_expression = self.parse() for token in parsed_expression: if token == '(': continue elif token == ')': rightOperand = self.expressionOperands.pop() leftOperand = self.expressionOperands.pop() operator = self.expressionOperators.pop() result = self._performOperation( leftOperand, rightOperand, operator) self._save(result, self.expressionOperands) elif token in self.expected_operators: token = self.expected_operators[token] self._save(token, self.expressionOperators) else: token = self._convert(token) self._save(token, self.expressionOperands) finalResult = self.expressionOperands.pop() return finalResult def _convert(self, token): return float(token) if '.' in token else int(token) def _performOperation(self, left, right, op): if op == '+': result = left + right elif op == '-': result = left - right elif op == '/': result = left / right else: result = left * right return result def _save(self, data, stack): stack.push(data) def parse(self): parsed_expression = [''] for token in self.exp: last_token = parsed_expression[-1] if (last_token.isdigit() or '.' in last_token or last_token == '') and (token not in self.expected_operators): parsed_expression[-1] += token else: parsed_expression.append(token) return parsed_expression[1:]
def setUp(self): self.n1 = Node(1) self.n2 = Node(2) self.n3 = Node(3) self.stack = LinkedListStack() self.stack.push(1) self.stack.push(2) self.stack.push(self.n1)
def test_size(self): # empty stack stack = LinkedListStack() self.assertEqual(stack.size(), 0) # one item stack.push('a') self.assertEqual(stack.size(), 1) # +1 item stack.push('b') self.assertEqual(stack.size(), 2)
def test_peek(self): # empty stack stack = LinkedListStack() with self.assertRaises(StackException): stack.peek() # add one thing then peek. it should still be there stack.push('a') self.assertEqual(stack.peek(), 'a') self.assertEqual(stack.peek(), 'a')
def test_is_empty(self): # empty stack stack = LinkedListStack() self.assertTrue(stack.is_empty()) # non-empty stack stack.push('a') self.assertFalse(stack.is_empty())
def test_push(self): """ We can push an item on the stack. We should not get an error """ stack = LinkedListStack() # nothing sent it with self.assertRaises(TypeError): stack.push() # add a bunch of items and push. no errors mixed_list = [1, 'a', [6, 7, 8], {"cat": "sylvester"}, None] for item in mixed_list: stack.push(item)
class TestStack(unittest.TestCase): def setUp(self): self.n1 = Node(1) self.n2 = Node(2) self.n3 = Node(3) self.stack = LinkedListStack() self.stack.push(1) self.stack.push(2) self.stack.push(self.n1) def test_peek(self): item = self.stack.peek() self.assertEqual(item, self.n1) def test_push(self): self.assertEqual(self.stack.get_size(), 3) self.stack.push(self.n2) self.assertEqual(self.stack.get_size(), 4) def test_pop(self): item = self.stack.pop() self.assertEqual(item, self.n1) def test_is_empty(self): empty_stack = LinkedListStack() self.assertEqual(empty_stack.is_empty(), True) self.assertEqual(self.stack.is_empty(), False) def test_get_size(self): empty_stack = LinkedListStack() self.assertEqual(empty_stack.get_size(), 0) self.assertEqual(self.stack.get_size(), 3)
def test_get_size(self): empty_stack = LinkedListStack() self.assertEqual(empty_stack.get_size(), 0) self.assertEqual(self.stack.get_size(), 3)
def test_is_empty(self): empty_stack = LinkedListStack() self.assertEqual(empty_stack.is_empty(), True) self.assertEqual(self.stack.is_empty(), False)
def setUp(self): self.stack = LinkedListStack()
class QueueTests(unittest.TestCase): def setUp(self): self.stack = LinkedListStack() def test_len_returns_0_for_empty_stack(self): self.assertEqual(len(self.stack), 0) def test_len_returns_correct_length_after_push(self): self.assertEqual(len(self.stack), 0) self.stack.push(2) self.assertEqual(len(self.stack), 1) self.stack.push(4) self.assertEqual(len(self.stack), 2) self.stack.push(6) self.stack.push(8) self.stack.push(10) self.stack.push(12) self.stack.push(14) self.stack.push(16) self.stack.push(18) self.assertEqual(len(self.stack), 9) def test_empty_pop(self): self.assertIsNone(self.stack.pop()) self.assertEqual(len(self.stack), 0) def test_pop_respects_order(self): self.stack.push(100) self.stack.push(101) self.stack.push(105) self.assertEqual(self.stack.pop(), 105) self.assertEqual(len(self.stack), 2) self.assertEqual(self.stack.pop(), 101) self.assertEqual(len(self.stack), 1) self.assertEqual(self.stack.pop(), 100) self.assertEqual(len(self.stack), 0) self.assertIsNone(self.stack.pop()) self.assertEqual(len(self.stack), 0)
class TestLinkedListStack(unittest.TestCase): def setUp(self): self.stack = LinkedListStack() def test_push(self): self.stack.push(1) self.assertEqual(self.stack.peek(), 1) def test_pop(self): popped = self.stack.pop() self.assertIs(popped, None) self.stack.push(1) popped = self.stack.pop() self.assertEqual(popped, 1) def test_full_stack(self): for i in range(10): self.stack.push(i) self.assertEqual(self.stack.peek(), 9) for i in range(10): self.assertEqual(self.stack.pop(), 9 - i) self.assertEqual(self.stack.pop(), None)
class TestLinkedListStack(unittest.TestCase): def setUp(self): self.st = LinkedListStack() def tearDown(self): del self.st # default feature test - push, pop, peek def test_01(self): self.st.push(1) self.st.push(2) self.st.push(3) self.assertEqual(self.st.peek(), 3) self.assertEqual(self.st.pop(), 3) self.assertEqual(self.st.peek(), 2) self.assertEqual(self.st.pop(), 2) self.assertEqual(self.st.peek(), 1) self.assertEqual(self.st.pop(), 1) # raise error when pop with empty stack def test_02(self): with self.assertRaises(IndexError) as cm: self.st.pop() self.assertEqual("stack is empty", str(cm.exception)) # test check empty def test_03(self): self.assertTrue(self.st.is_empty())
def test_pop(self): """ We can pop an item from the stack """ # empty stack stack = LinkedListStack() with self.assertRaises(StackException): stack.pop() # add one thing then pop stack.push('a') self.assertEqual(stack.pop(), 'a') with self.assertRaises(StackException): stack.pop() # add 3 things then pop stack.push('a') stack.push('b') stack.push('c') self.assertEqual(stack.pop(), 'c') self.assertEqual(stack.pop(), 'b') self.assertEqual(stack.pop(), 'a') with self.assertRaises(StackException): stack.pop()