class StackTest(unittest.TestCase): def setUp(self) -> None: self.__stack = Stack() self.__stack.push(1) self.__stack.push(2) self.__stack.push(3) self.__stack.push(4) self.__stack.push(5) def testPop(self) -> None: self.assertIn(5, self.__stack.pop()) self.assertEqual(4, len(self.__stack)) def testPush(self) -> None: self.__stack.push(6) self.assertIn(6, self.__stack.pop()) self.assertEqual(5, len(self.__stack)) def testFindKey(self) -> None: self.assertTrue(self.__stack.findKey(1)) self.assertFalse(self.__stack.findKey(999999)) def testPushEmptyKey(self) -> None: with self.assertRaises(Exception): self.__stack.push(None) def testPopAll(self) -> None: for _ in range(6): item = self.__stack.pop() self.assertTrue(not item)
def __dfsFromVertex(self, vertex: V, visited: Set[V], stack: Stack) -> None: if vertex is None or visited is None or stack is None: raise Exception( "Invalid input: vertex: {}, visited: {}, stack: {}".format( vertex, visited, stack)) for neighbor, _ in self.__adjacencyMatrix.allSuccessors(vertex): if neighbor in visited: continue visited.add(neighbor) self.__dfsFromVertex(neighbor, visited, stack) stack.push(vertex)
#! /usr/local/bin/python from common import table from common import Stack import sys task2 = table(sys.argv[1]) mx = task2.construct() for line in open(sys.argv[2], 'rU'): trig = 0 stk = Stack() stk.push('S $') # print '***',stk.prt() # line=line[:-1] line = line[:-1] + '$' # line+='$' print '++', line index = 0 a = line[index] while not stk.is_empty(): print '--', stk.prt() # print '||',a X = stk.top() if X.isupper(): # print 'run 1','|',X,'|',a,'|',mx.get(X,a) if mx.get(X, a) != -1 and mx.get(X, a) != None: print mx.get(X, a) stk.pop() stk.push(mx.get(X, a)[0]) else: print 'reject 1' trig = 1
#! /usr/local/bin/python from common import table from common import Stack import sys task2=table(sys.argv[1]) mx=task2.construct() #stk=Stack() #stk.push('S $') for line in open(sys.argv[2],'rU'): trig=0 stk=Stack() stk.push('S $') print '***',stk.prt() line=line[:-1] line+='$' print '++',line index=0 a=line[index] while not stk.is_empty(): print '--',stk.prt() # print '||',a X=stk.top() if X.isupper(): print 'run 1','|',X,'|',a,'|',mx.get(X,a) if mx.get(X,a)!=-1 and mx.get(X,a)!=None: print mx.get(X,a) stk.pop() stk.push(mx.get(X,a)[0]) else: print 'reject 1'
#! /usr/local/bin/python from common import Stack ss = Stack() ss.push('abc') ss.push('def') print ss.prt()
#! /usr/local/bin/python from common import Stack ss=Stack() ss.push('abc') ss.push('def') print ss.prt()