예제 #1
0
파일: test_stack.py 프로젝트: rmartind/ctci
class TestStackIsEmpty(unittest.TestCase):
    
    def setUp(self):
        self.a = Stack()

    def test_empty(self):
        self.assertTrue(self.a.is_empty())

    def test_not_empty(self):
        self.a.push(1)
        self.assertFalse(self.a.is_empty())
예제 #2
0
 def setUp(self):
     self.a = Stack()
     self.b = Stack()
     self.b.push(76)
     self.b.push(58)
     self.b.push(49)
     self.b.push(36)
     self.b.push(26)
     self.b.push(22)
     self.b.push(21)
     self.b.push(10)
     self.b.push(9)
     self.b.push(8)
     self.b.push(2)
     self.b.push(1)
예제 #3
0
파일: sortstack.py 프로젝트: rmartind/ctci
def sort_stack(unsorted):
    temp = Stack()
    temp.push(unsorted.pop())
    while not unsorted.is_empty():
        popped = unsorted.pop()
        while popped > temp.peek():
            unsorted.push(temp.pop())
        temp.push(popped)
    return temp
예제 #4
0
파일: test_stack.py 프로젝트: rmartind/ctci
class TestStackPeek(unittest.TestCase):

    def setUp(self):
        self.a = Stack()

    def test_one(self):
        self.a.push(1)
        self.assertEqual(self.a.peek(), 1)

    def test_two(self):
        self.a.push(1)
        self.a.push(4)
        self.assertEqual(self.a.peek(), 4)
예제 #5
0
class TestSortStack(unittest.TestCase):
    
    def setUp(self):
        self.a = Stack()
        self.b = Stack()
        self.b.push(76)
        self.b.push(58)
        self.b.push(49)
        self.b.push(36)
        self.b.push(26)
        self.b.push(22)
        self.b.push(21)
        self.b.push(10)
        self.b.push(9)
        self.b.push(8)
        self.b.push(2)
        self.b.push(1)

    def test_one(self):
        self.a.push(1)
        self.a.push(22)
        self.a.push(8)
        self.a.push(10)
        self.a.push(26)
        self.a.push(58)
        self.a.push(21)
        self.a.push(36)
        self.a.push(9)
        self.a.push(49)
        self.a.push(2)
        self.a.push(76)

        self.a = sortstack.sort_stack(self.a)

        while not self.a.is_empty():
            self.assertTrue(self.a.pop() == self.b.pop())
예제 #6
0
파일: test_stack.py 프로젝트: rmartind/ctci
 def setUp(self):
     self.a = Stack()
예제 #7
0
파일: test_stack.py 프로젝트: rmartind/ctci
class TestStackLen(unittest.TestCase):
    
    def setUp(self):
        self.a = Stack()

    def test_empty(self):
        self.assertEqual(len(self.a), 0)

    def test_one(self):
        self.a.push(1)
        self.assertEqual(len(self.a), 1)

    def test_multi(self):
        self.a.push(1)
        self.a.push(1)
        self.a.push(1)
        self.a.push(1)
        self.assertEqual(len(self.a), 4)
    
    def test_with_pop(self):
        self.a.push(1)
        self.a.pop()
        self.a.push(1)
        self.a.push(1)
        self.assertEqual(len(self.a), 2)

    def test_with_pop_multi(self):
        self.a.push(1)
        self.a.pop()
        self.a.push(1)
        self.a.push(1)
        self.a.pop()
        self.assertEqual(len(self.a), 1)
예제 #8
0
 def __init__(self):
     self._main = Stack()
     self._temp = Stack()
     self._prior_remove = False
예제 #9
0
class QueueViaStacks(Stack):
    def __init__(self):
        self._main = Stack()
        self._temp = Stack()
        self._prior_remove = False

    def add(self, payload):
        if self._prior_remove:
            self._prior_remove = False
            while not self._temp.is_empty():
                self._main.push(self._temp.pop())
        self._main.push(payload)

    def remove(self):
        if self._prior_remove:
            return self._temp.pop()

        while not self._main.is_empty():
            self._temp.push(self._main.pop())
        self._prior_remove = True
        return self._temp.pop()

    def peek(self):
        if self._prior_remove:
            return self._temp.peek()

        while not self._main.is_empty():
            self._temp.push(self._main.pop())
        self._prior_remove = True
        return self._temp.peek()

    def is_empty(self):
        return len(self._main) == 0 and len(self._temp) == 0