-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_18_queues_and_stacks.py
52 lines (42 loc) · 1.25 KB
/
day_18_queues_and_stacks.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
import sys
# Could have just did
# def palindrome(word):
# return word.lower() == word[::-1].lower()
from collections import deque
class Solution:
def __init__(self):
self.stack = deque()
self.queue = deque()
def pushCharacter(self, string_stack):
self.stack.append(string_stack)
def enqueueCharacter(self, string_queue):
self.queue.append(string_queue)
def popCharacter(self):
return self.stack.pop()
def dequeueCharacter(self):
return self.queue.popleft()
# pop(0) if it was a list
# read the string s
s = 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:
print("The word, " + s + ", is a palindrome.")
else:
print("The word, " + s + ", is not a palindrome.")