-
Notifications
You must be signed in to change notification settings - Fork 0
/
queueFromStacks.py
62 lines (53 loc) · 1.33 KB
/
queueFromStacks.py
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
class Queue(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stackOrig = []
self.stackCopy = []
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.stackOrig.append(x)
def pop(self):
"""
:rtype: nothing
"""
if self.empty():
raise "No elements found in queue"
return None
self.moveStackOrigToCopy()
val = self.stackCopy[len(self.stackCopy) - 1]
self.stackCopy = self.stackCopy[0:len(self.stackCopy) - 1]
return val
def peek(self):
"""
:rtype: int
"""
if self.empty():
raise "No elements found in queue"
return None
self.moveStackOrigToCopy()
return self.stackCopy[len(self.stackCopy) - 1]
def empty(self):
"""
:rtype: bool
"""
if len(self.stackOrig) + len(self.stackCopy) == 0:
return True
return False
def moveStackOrigToCopy(self):
if len(self.stackCopy) == 0:
while len(self.stackOrig) != 0:
self.stackCopy.append(self.stackOrig.pop())
q = Queue()
q.push(1)
q.push(2)
q.push(3)
q.pop()
q.pop()
q.pop()
#print q.peek()
print q.empty()