Beispiel #1
0
class Queueky():
    def __init__(self):
        self.__s = Stack()

    def enqueue(self, elem):
         self.__s.push(elem)

    def dequeue(self):
        temp = Stack()
        cache = Stack()
        while self.__s.count() > 1:
            temp.push(self.__s.pop())
        while temp.count() > 0:
            cache.push(temp.pop())
        result = self.__s.pop()
        self.__s = cache    
        return result

    def is_empty(self):
        return self.__s.is_empty()

    def top(self):
        return self.__s.top()

    def count(self):
        return self.__s.count()
Beispiel #2
0
def test_stack_count():
	s = Stack()
	assert_equals(s.count(), 0)
	s.push(1)
	assert_equals(s.count(), 1)
	s.pop()
	assert_equals(s.count(), 0)
 def test_count(self):
     '''Stack - Count items on the stack'''
     stack = Stack()
     self.assertEqual(0, stack.count())
     stack.push("One")
     self.assertEqual(1, stack.count())
     stack.push("Two")
     self.assertEqual(2, stack.count())
 def test_push(self):
     '''Stack - Push an item onto the stack'''
     stack = Stack()
     self.assertEqual(stack.count(), 0)
     stack.push("pete")
     topval = stack.peek()
     self.assertEqual(stack.count(), 1)
     self.assertEqual("pete", topval)
 def test_pop(self):
     '''Stack - Pop an item from the stack'''
     stack = Stack()
     stack.push("Wendy")
     stack.push("Peter")
     stack.dump()
     self.assertEqual(stack.count(), 2)
     answer = stack.pop()
     self.assertEqual(answer, "Peter")
     self.assertEqual(stack.count(), 1)
Beispiel #6
0
 def test_features(self):
     stk = Stack()
     stk.push(3)
     stk.push(12)
     stk.push(5)
     stk.push(17)
     self.assertEqual(stk.count(), 4)
     self.assertEqual(stk.peek(), 17)
     self.assertEqual(stk.pop(),17)
     self.assertEqual(stk.count(), 3)
Beispiel #7
0
def test_peek():
	s = Stack()
	assert_equals(s.peek(), None)
	s.push(1)
	assert_equals(s.peek(), 1)
	assert_equals(s.count(), 1)
	assert_equals(s.pop(), 1)
Beispiel #8
0
def test_is_empty():
	s = Stack()
	assert_equals(s.is_empty(), True)
	s.push(1)
	assert_equals(s.is_empty(), False)
	s.pop()
	assert_equals(s.is_empty(), True)
	assert_equals(s.count(), 0)
Beispiel #9
0
 def dequeue(self):
     temp = Stack()
     cache = Stack()
     while self.__s.count() > 1:
         temp.push(self.__s.pop())
     while temp.count() > 0:
         cache.push(temp.pop())
     result = self.__s.pop()
     self.__s = cache    
     return result
Beispiel #10
0
class TestStack(unittest.TestCase):
    def setUp(self):
        self.client = Redis()
        self.client.flushdb()

        self.stack = Stack(self.client, "test-stack")

    def test_push(self):
        self.assertEqual(self.stack.push(1), 1)
        self.assertEqual(self.stack.push(2), 2)

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

    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_count(self):
        self.assertEqual(self.stack.count(), 0)
        self.stack.push(1)
        self.assertEqual(self.stack.count(), 1)
Beispiel #11
0
def checkPharenthesis(question):
  ans = True
  stack = Stack()

  for character in question:
    if isOpenPharenthesis(character):
      stack.push(character)
    else:
      try:
        lastOpen = stack.pop()
        if not isCorrectClosedType(lastOpen, character):
          raise Exception("[Error] Wrong close pharenthesis type.")
      except Exception:
        print("%s is in wrong format!" % question)
        ans = False
        break

  if stack.count() > 0:
    ans = False
    print("%s is in wrong format!" % question)

  if ans:
    print("%s format is correct!" % question)
Beispiel #12
0
def test_empty_stack():
	s = Stack()
	assert_equals(s.is_empty(), True)
	assert_equals(s.count(), 0)
	assert_equals(s.pop(), None)
Beispiel #13
0
from stack import Stack

if __name__=="__main__":
	stack = Stack()
	stack.push(1)
	stack.push(2)
	stack.push(3)
	stack.show()
	stack.pop()
	stack.show()
	print(stack.peek())
	print(stack.count())
	stack.show()
Beispiel #14
0
from stack import Stack

print(help(Stack))

s = Stack()

print("=========================")
print(s.top)
print(s.count())
print(s.full())
print(s.empty())
for i in s:
    print("%s in s" % i)

s.push(2)

print("=========================")
print(s.top)
print(s.count())
print(s.full())
print(s.empty())
for i in s:
    print("%s in s" % i)

s.push(4)

print("=========================")
print(s.top)
print(s.count())
print(s.full())
print(s.empty())
Beispiel #15
0
def test_push():
    colors = Stack()
    colors.push("Pthalo Blue")
    assert colors.count() == 1
    colors.push("Ultramarine Blue")
    assert colors.count() == 2
class TestStack(unittest.TestCase):
    def setUp(self):
        self.stack = Stack()

    def test_str(self):
        self.assertEqual(self.stack.__str__(), '')
        self.stack.push(1)
        self.assertEqual(self.stack.__str__(), '1 ')
        self.stack.push(3)
        self.assertEqual(self.stack.__str__(), '3 1 ')

    def test_push(self):
        with self.assertRaisesRegex(Exception, "No value has been found"):
            self.stack.find(1)
        self.stack.push(1)
        self.assertEqual(self.stack.find(1), True)
        self.stack.push(2)
        self.assertEqual(self.stack.find(2), True)
        self.assertEqual(self.stack.count(), 2)

    def test_pop(self):
        with self.assertRaisesRegex(Exception, "Your list is empty."):
            self.stack.pop()
        self.stack.push(1)
        self.assertEqual(self.stack.find(1), True)
        self.stack.pop()
        with self.assertRaisesRegex(Exception, "No value has been found."):
            self.stack.find(1)

    def test_find(self):
        with self.assertRaisesRegex(Exception, "No value has been found."):
            self.stack.find(1)
        self.stack.push(1)
        self.stack.push(2)
        self.assertEqual(self.stack.find(1), True)
        self.assertEqual(self.stack.find(2), True)
        with self.assertRaisesRegex(Exception, "No value has been found."):
            self.stack.find(3)

    def test_count(self):
        self.assertEqual(self.stack.count(), 0)
        self.stack.push(1)
        self.assertEqual(self.stack.count(), 1)
        self.stack.push(1)
        self.assertEqual(self.stack.count(), 2)
        self.stack.pop()
        self.assertEqual(self.stack.count(), 1)

    def test_is_empty(self):
        self.assertEqual(self.stack.is_empty(), True)
        self.stack.push(1)
        self.assertEqual(self.stack.is_empty(), False)

    def test_getmin(self):
        with self.assertRaisesRegex(Exception, "Your Stack is empty."):
            self.stack.getmin()
        self.stack.push(2)
        self.assertEqual(self.stack.getmin(), 2)
        self.stack.push(1)
        self.assertEqual(self.stack.getmin(), 1)
        self.stack.push(0)
        self.assertEqual(self.stack.getmin(), 0)