예제 #1
0
def test_stack_push_pop():
    el = 1
    stack = Stack(5)
    stack.push(el)

    assert stack.size() == 1
    assert stack.pop() == el
    assert stack.size() == 0
예제 #2
0
class Queue:

    def __init__(self):
        self.s1 = Stack()
        self.s2 = Stack()

    def enqueue(self, val):
        while self.s1.size() > 0:
            self.s2.push(self.s1.pop())
        self.s2.push(val)
        while self.s2.size() > 0:
            self.s1.push(self.s2.pop())

    def dequeue(self):
        return self.s1.pop()
예제 #3
0
def stack_reverse_string(input):
    stk = Stack()
    for elem in input:
        stk.push(elem)
    res = ""
    while stk.size() > 0:
        elem = stk.pop()
        res += elem
    return res
예제 #4
0
def matching_parens(input):
    stk = Stack()

    for elem in input:
        if elem in paren_map.keys():
            stk.push(elem)
        else:
            if stk.size() == 0:
                return False
            match = stk.pop()
            if not paren_map[match] == elem:
                return False
    return True
def reverseString(mystr):
    """
    Reverses the input string using Stack data structure
    
    INPUT
    -------
        mystr : apple
    RETURN
    -------
        elppa
        
    """
    revStr = ''
    temp = Stack()

    for token in mystr:
        temp.push(token)

    for i in range(temp.size()):
        revStr += temp.pop()

    return revStr
예제 #6
0
class TestStack(TestCase):
    def setUp(self):
        super().setUp()
        self.stack = Stack()

    def test_pop(self):
        self.stack.push(1)
        self.stack.push(2)

        self.assertEqual(self.stack.pop(), 2)
        self.assertEqual(self.stack.pop(), 1)

    def test_pop_empty(self):
        self.assertIsNone(self.stack.pop())

    def test_peek(self):
        self.stack.push(1)
        self.stack.push(2)

        self.assertEqual(self.stack.peek(), 2)
        self.stack.pop()
        self.assertEqual(self.stack.peek(), 1)

    def test_size(self):
        self.stack.push(1)
        self.stack.push(2)

        self.assertEqual(self.stack.size(), 2)

    def test_is_empty(self):
        self.assertTrue(self.stack.is_empty())

        self.stack.push(1)

        self.assertFalse(self.stack.is_empty())

        self.stack.pop()