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
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
- Create a Stack class to represent a stack data structure.
- Define a palindrome function that takes an input string input_str.
- Create an empty stack.
- Push each character in input_str onto the stack.
- Pop each character off the stack and compare it to the corresponding character in input_str.
- If any characters don't match, isPalindrome is set to False.
- Return isPalindrome.
- In the main function, test the palindrome function with an input string and print the result.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images