Created
July 29, 2016 11:32
-
-
Save DeviaVir/8261a2008fe0dcd11596421784eb9de8 to your computer and use it in GitHub Desktop.
Detect word palindromes
This file contains hidden or 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
import sys | |
from Queue import Queue | |
class Solution: | |
def __init__(self,): | |
self.stack = [] | |
self.queue = Queue() | |
def pushCharacter(self, ch): | |
self.stack.append(ch) | |
def enqueueCharacter(self, ch): | |
self.queue.put(ch) | |
def popCharacter(self): | |
return self.stack.pop() | |
def dequeueCharacter(self): | |
return self.queue.get() | |
# read the string s | |
s=raw_input() | |
#Create the Solution class object | |
obj=Solution() | |
l=len(s) | |
# push/enqueue all the characters of string s to stack | |
for i in range(l): | |
obj.pushCharacter(s[i]) | |
obj.enqueueCharacter(s[i]) | |
isPalindrome=True | |
''' | |
pop the top character from stack | |
dequeue the first character from queue | |
compare both the characters | |
''' | |
for i in range(l / 2): | |
if obj.popCharacter()!=obj.dequeueCharacter(): | |
isPalindrome=False | |
break | |
#finally print whether string s is palindrome or not. | |
if isPalindrome: | |
sys.stdout.write ("The word, "+s+", is a palindrome.") | |
else: | |
sys.stdout.write ("The word, "+s+", is not a palindrome.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment