Created
March 18, 2017 00:42
-
-
Save cyyeh/ebc506d583aa5a78bbd15e741b375bea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Deque: | |
def __init__(self): | |
self.items = [] | |
def isEmpty(self): | |
return self.items == [] | |
def addFront(self, item): | |
self.items.append(item) | |
def addRear(self, item): | |
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) | |
# application1 using Deque: Palindrome Checker | |
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 | |
def main(): | |
print(palchecker("lsdkjfskf")) | |
print(palchecker("radar")) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment