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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education