-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue-using-stacks.py
More file actions
79 lines (63 loc) · 2.32 KB
/
queue-using-stacks.py
File metadata and controls
79 lines (63 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# https://leetcode.com/problems/implement-queue-using-stacks/
# Implement the following operations of a queue using stacks.
#
# push(x) -- Push element x to the back of queue.
# pop() -- Removes the element from in front of queue.
# peek() -- Get the front element.
# empty() -- Return whether the queue is empty.
# Notes:
# You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
# Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
# You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
import unittest
class Queue(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.main_stack = []
self.auxillary_stack = []
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
if len(self.main_stack) == 0 :
self.main_stack.append(x)
else:
for i in xrange(len(self.main_stack)):
self.temp_data = self.main_stack.pop()
self.auxillary_stack.append(self.temp_data)
self.main_stack.append(x)
for i in xrange(len(self.auxillary_stack)):
self.temp_data = self.auxillary_stack.pop()
self.main_stack.append(self.temp_data)
def pop(self):
"""
:rtype: nothing
"""
return self.main_stack.pop()
def peek(self):
"""
:rtype: int
"""
return self.main_stack[len(self.main_stack)-1]
def empty(self):
"""
:rtype: bool
"""
if len(self.main_stack)==0:
return True
return False
#return self.main_stack[len(self.main_stack)-1] ==0
class TestQueueUsingStacks(unittest.TestCase):
my_queue = Queue()
def test_empty_function(self):
self.assertTrue(self.my_queue.empty())
def test_peek_function(self):
self.my_queue.push(6)
self.assertEquals(self.my_queue.peek(),6)
def test_pop_function(self):
self.assertEquals(self.my_queue.pop(),6)
if __name__ == "__main__":
unittest.main()