forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101_10.py
More file actions
63 lines (51 loc) · 1.56 KB
/
101_10.py
File metadata and controls
63 lines (51 loc) · 1.56 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
################### Stack and Queue Data Structure
import system
class Solution:
# Stack Primitives
def __init__(self):
self.stack = []
self.queue = []
def pushCharacter(self,item):
self.stack.append(item)
return
def enqueueCharacter(self,item):
n = len(self.queue)
self.queue.append(item)
return
def popCharacter(self):
item = self.stack[len(self.stack)-1]
del self.stack[len(self.stack)-1]
return item
def dequeueCharacter(self):
if len(self.queue) >= 1:
item = self.queue[0]
del self.queue[0]
# Auto shift left for deletion in a python list!
#for i in range(1,len(self.queue)):
# self.queue[i-1] = self.queue[i]
#print "out",item, self.queue
return item
# 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.")