A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate.
from cgitb import text
from os import popen
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self, data):
self.items.append(data)
def pop(self):
return self.items.pop()
s = Stack()
input_text = input('Please enter the string: ')
for character in input_text:
s.push(character)
textrev = ''
while not s.is_empty():
textrev = textrev + s.pop()
if input_text == textrev:
print('True')
else:
print('False')
output
input: pop
output: True
input: clear
output: False
Comments
Are you sure about the answer?
Leave a comment