Write a program that uses a stack to test input strings to determine whether they are palindromes. A palindrome is a sequence of words that reads the same as the sequence in reverse: for example, noon.   Use this Python Example : class Stack: ''' TODO: Remove the "pass" statements and implement each method Add any methods if necesssary DON'T use any builtin stack class to store your items ''' def __init__(self): # Constructor function pass def isEmpty(self): # Returns True if the stack is empty or False otherwise pass def len(self): # Returns the number of items in the stack pass def peek(self): # Returns the item at the top of the stack pass def push(self, item): # Adds item to the top of the stack pass def pop(self): # Removes and returns the item at the top of the stack pass def palindrome(input_str): isPalindrome = False #TODO: Your work here # Return if input_str is palindrome or not return isPalindrome if __name__ == "__main__": my_str = "noon" print(palindrome(my_str)) # Correct Output: True #TODO (optional): Your testing code here

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

Write a program that uses a stack to test input strings to determine whether they
are palindromes. A palindrome is a sequence of words that reads the same as the
sequence in reverse: for example, noon.

 

Use this Python Example :

class Stack:
'''
TODO: Remove the "pass" statements and implement each method
Add any methods if necesssary
DON'T use any builtin stack class to store your items
'''
def __init__(self): # Constructor function
pass
def isEmpty(self): # Returns True if the stack is empty or False otherwise
pass
def len(self): # Returns the number of items in the stack
pass
def peek(self): # Returns the item at the top of the stack
pass
def push(self, item): # Adds item to the top of the stack
pass
def pop(self): # Removes and returns the item at the top of the stack
pass
def palindrome(input_str):
isPalindrome = False
#TODO: Your work here
# Return if input_str is palindrome or not
return isPalindrome
if __name__ == "__main__":
my_str = "noon"
print(palindrome(my_str)) # Correct Output: True
#TODO (optional): Your testing code here

Expert Solution
Algorithm
  1. Create a Stack class to represent a stack data structure.
  2. Define a palindrome function that takes an input string input_str.
  3. Create an empty stack.
  4. Push each character in input_str onto the stack.
  5. Pop each character off the stack and compare it to the corresponding character in input_str.
  6. If any characters don't match, isPalindrome is set to False.
  7. Return isPalindrome.
  8. In the main function, test the palindrome function with an input string and print the result.
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning