示例#1
0
class TestStack(unittest.TestCase):
    def setUp(self):
        self.stack = Stack()

    def tearDown(self):
        return super().tearDown()

    def test_size(self):
        self.assertEqual(self.stack.size(), 0)

    def test_push(self):
        for i in range(10):
            self.stack.push(i)

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

    def test_pop(self):
        for i in range(10):
            self.stack.push(i)

        self.assertEqual(self.stack.pop(), 9)

    def test_top(self):
        self.stack.push(1)
        self.assertEqual(self.stack.top(), 1)

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

    def test_top_exception(self):
        self.assertIsNone(self.stack.top())
示例#2
0
 def test_push_five_elements_to_stack_and_pop_two_elements(self):
     stack = Stack()
     langs = ['python', 'java', 'ruby', 'php', 'go']
     for lang in langs:
         stack.push(lang)
     self.assertEqual(len(langs), stack.size())
     element1 = stack.pop()
     element2 = stack.pop()
     self.assertEqual('go', element1)
     self.assertEqual('php', element2)
     self.assertEqual(3, stack.size())
示例#3
0
 def test_push_five_elements_and_iterate_stack(self):
     stack = Stack()
     langs = ['python', 'java', 'ruby', 'php', 'go']
     for lang in langs:
         stack.push(lang)
     self.assertEqual(len(langs), stack.size())
     for index, element in enumerate(stack):
         self.assertEqual(element, langs[len(langs) - index - 1])
示例#4
0
 def test_push_five_elements_to_stack_and_pop_all_elements(self):
     stack = Stack()
     langs = ['python', 'java', 'ruby', 'php', 'go']
     for lang in langs:
         stack.push(lang)
     self.assertEqual(len(langs), stack.size())
     for i in range(len(langs)):
         element = stack.pop()
         self.assertEqual(element, langs[len(langs) - i - 1])
     self.assertTrue(stack.is_empty())
class BufferedQueue:
    def __init__(self, limit):
        self.limit = limit
        self.enqueued = Stack()
        self.dequeued = Stack()

    def __str__(self):
        return self.enqueued.__str__() + self.dequeued.__str__(
        ) + f' buffer limit {self.limit}'

    def enqueue(self, item):
        if self.size() < self.limit:
            self.enqueued.push(item)
            return True
        else:
            return False

    def dequeue(self):
        if self.dequeued.size() > 0:
            return self.dequeued.pop()
        while self.enqueued.size() > 1:
            self.dequeued.push(self.enqueued.pop())
        return self.enqueued.pop()

    def next(self):
        if self.dequeued.size() > 0:
            return self.dequeued.last()
        else:
            return self.enqueued.first()

    def last(self):
        if self.enqueued.size() > 0:
            return self.enqueued.last()
        else:
            return self.dequeued.first()

    def size(self):
        return self.enqueued.size() + self.dequeued.size()
示例#6
0
#!/usr/bin/env python3
""" Laboratory work 2. Task 1 """

from stack.stack import Stack

s = Stack([5, 7, 4, 9, 1, 6, 2])

print(s.push())
print("Deleted item from Stack:", s.pop())
print("Last item in Stack:", s.back())
print("Size of stack:", s.size())
print(s.clear())
print("Bye!")
from stack.stack import Stack
from stack.stack import foo

stk = Stack()
stk.push(1)
stk.push(2)
stk.pop()
stk.push(3)
print stk.peek()
print stk.size()

print foo()
示例#8
0
 def test_add_one_element_to_stack(self):
     stack = Stack()
     stack.push('python')
     self.assertEqual(1, stack.size())
示例#9
0
from stack.stack import Stack
from stack.stack import foo


stk = Stack()
stk.push(1)
stk.push(2)
stk.pop()
stk.push(3)
print stk.peek()
print stk.size()


print foo()