def infix_to_potfix(token_list): """ 들어온 중위 표현식을 후위 표현식으로 변경 :param token_list: :return: 후위 표현식 """ prec = {'*': 3, '/': 3, '+': 2, '-': 2, '(': 1} opStack = ArrayStack() result = [] for token in token_list: if isinstance(token, int): result.append(token) elif token == '(': opStack.push(token) elif token == ')': while opStack.peek() != '(': result.append(opStack.pop()) opStack.pop() else: if not opStack.isEmpty(): if prec[opStack.peek()] >= prec[token]: result.append(opStack.pop()) opStack.push(token) else: opStack.push(token) else: opStack.push(token) while not opStack.isEmpty(): result.append(opStack.pop()) print(result) return result
class TestArrayStack(unittest.TestCase): def setUp(self): self.st = ArrayStack() 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()) # test expand def test_04(self): self.assertEqual(len(self.st._array), 10) for i in range(11): self.st.push(i) self.assertEqual(len(self.st._array), 20)
def test_peek(self): # empty stack stack = ArrayStack() 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 solveMaze(row, column, maze): stack = ArrayStack() stack.push((row, column)) while not stack.isEmpty(): (row, column) = stack.peek() if maze[row][column] == 'T': return stack elif maze[row][column] != '.': maze[row][column] = '.' counter = 0 ###adjacent cells based on changing x,y values so need 4 options if row != 0 and not maze[row - 1][column] in ('*', '.'): stack.push((row-1, column)) counter += 1 if row + 1 != maze.getHeight() and not maze[row + 1][column] in ('*', '.'): stack.push((row + 1, column)) counter += 1 if column + 1 != maze.getWidth() and not maze[row][column + 1] in ('*', '.'): stack.push((row, column + 1)) counter += 1 if column != 0 and not maze[row][column -1] in ('*', '.'): stack.push((row, column - 1)) counter += 1 if counter == 0: stack.pop() return None
class StackQueue: def __init__(self): self.stack1 = ArrayStack() self.stack2 = ArrayStack() self.count = 0 def enqueue(self, item): self.stack1.push(item) self.count += 1 def dequeue(self): if self.is_empty(): raise ValueError("Empty Queue Error") self.moveStack1ToStack2() return self.stack2.pop() def peek(self): if self.is_empty(): raise ValueError("Empty Queue Error") self.moveStack1ToStack2() return self.stack2.peek() def moveStack1ToStack2(self): if self.stack2.is_empty(): while not self.stack1.is_empty(): self.stack2.push(self.stack1.pop()) def is_empty(self): return self.stack1.is_empty() and self.stack2.is_empty()