-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.py
42 lines (27 loc) · 945 Bytes
/
deque.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
class Deque: # like a deck of cards on a table
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def addFront(self, item): # plop new card right on top O(1)
self.items.append(item)
def addRear(self, item): # lift the whole deck to place a new card underneath the deck O(n)
self.items.insert(0, item)
def removeFront(self):
return self.items.pop()
def removeRear(self):
return self.items.pop(0)
def size(self):
return len(self.items)
""" some tasks where deque would come in handy """
def palchecker(aString):
chardeque = Deque()
for ch in aString:
chardeque.addRear(ch)
stillEqual = True
while chardeque.size() > 1 and stillEqual:
first = chardeque.removeFront()
last = chardeque.removeRear()
if first != last:
stillEqual = False
return stillEqual