【hackerrank】-Day 18: Queues and Stacks
30-queues-stacks
Welcome to Day 18! Today we’re learning about Stacks and Queues. Check out the Tutorial tab for learning materials and an instructional video!
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backwards and forwards. Can you determine if a given string, , is a palindrome?
To solve this challenge, we must first take each character in , enqueue it in a queue, and also push that same character onto a stack. Once that’s done, we must dequeue the first character from the queue and pop the top character off the stack, then compare the two characters to see if they are the same; as long as the characters match, we continue dequeueing, popping, and comparing each character until our containers are empty (a non-match means isn’t a palindrome).
Write the following declarations and implementations:
Two instance variables: one for your , and one for your .
A void pushCharacter(char ch) method that pushes a character onto a stack.
A void enqueueCharacter(char ch) method that enqueues a character in the instance variable.
A char popCharacter() method that pops and returns the character at the top of the instance variable.
A char dequeueCharacter() method that dequeues and returns the first character in the instance variable.
Input Format
You do not need to read anything from stdin. The locked stub code in your editor reads a single line containing string . It then calls the methods specified above to pass each character to your instance variables.
Constraints
is composed of lowercase English letters.
Output Format
You are not responsible for printing any output to stdout.
If your code is correctly written and is a palindrome, the locked stub code will print ; otherwise, it will print
Sample Input
racecar
Sample Output
The word, racecar, is a palindrome.
import sys
class Solution:
# Write your code here
def __init__(self):
# self.size = size
self.stack = []
self.top = -1
self.front = -1
self.rear = -1
self.queue = []
def pushCharacter(self, strr):
if self.isfull_stack():
raise Exception("stack is full")
else:
self.stack.append(strr)
self.top = self.top + 1
def popCharacter(self):
if self.isempty_stack():
raise Exception("stack is empty")
else:
self.top = self.top - 1
last = self.stack.pop()
return last
def isfull_stack(self):
# return self.top + 1 == self.size
return 0
def isempty_stack(self):
return self.top == -1
def show_stack(self):
return self.stack
def enqueueCharacter(self, strr):
if self.isfull_queue():
raise Exception("queue is full")
else:
self.queue.append(strr)
self.rear = self.rear + 1
def dequeueCharacter(self):
if self.isempty_queue():
raise Exception("queue is empty")
else:
first = self.queue.pop(0)
self.front = self.front + 1
return first
def isfull_queue(self):
# return self.rear - self.front + 1 == self.size
return 0
def isempty_queue(self):
return self.rear == self.front
def show_queue(self):
return self.queue
# 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.")
Congratulations!
You have passed the sample test cases. Click the submit button to run your code against all the test cases.
Testcase 0 Testcase 1
Input (stdin)
racecar
Your Output (stdout)
The word, racecar, is a palindrome.
Expected Output
The word, racecar, is a palindrome.
上一篇: qml 3d场景加载的两种方式
下一篇: SpringMVC——参数绑定时中文乱码