Question: def is_palindrome_stack(string): ------------------------------------------------------- Determines if string is a palindrome. Ignores case, digits, spaces, and punctuation in string. Use: palindrome = is_palindrome_stack(string) ------------------------------------------------------- Parameters: string
def is_palindrome_stack(string): """ ------------------------------------------------------- Determines if string is a palindrome. Ignores case, digits, spaces, and punctuation in string. Use: palindrome = is_palindrome_stack(string) ------------------------------------------------------- Parameters: string - a string (str) Returns: palindrome - True if string is a palindrome, False otherwise (bool) ------------------------------------------------------- """
The function must use one and only one stack to solve the problem. Show enough tests to cover two palindromes and an non-palindrome.
Examples of valid palindromes:
- racecar
- Otto
- Able was I ere I saw Elba
- A man, a plan, a canal, Panama!
The Class Stack :
class Stack:
def __init__(self): """ ------------------------------------------------------- Initializes an is_empty stack. Data is stored in a Python list. Use: s = Stack() ------------------------------------------------------- Returns: a new Stack object (Stack) ------------------------------------------------------- """ self._values = []
def is_empty(self): """ ------------------------------------------------------- Determines if the stack is empty. Use: b = s.is_empty() ------------------------------------------------------- Returns: True if the stack is empty, False otherwise ------------------------------------------------------- """ if len(self._values) >0: a = False else: a = True return a
def push(self, value): """ ------------------------------------------------------- Pushes a copy of value onto the top of the stack. Use: s.push(value) ------------------------------------------------------- Parameters: value - a data element (?) Returns: None ------------------------------------------------------- """
self._values.append(deepcopy(value))# Your code here return def pop(self): """ ------------------------------------------------------- Pops and returns the top of stack. The value is removed from the stack. Attempting to pop from an empty stack throws an exception. Use: value = s.pop() ------------------------------------------------------- Returns: value - the value at the top of the stack (?) ------------------------------------------------------- """ assert len(self._values) > 0, "Cannot pop from an empty stack" value = self._values.pop() return value # Your code here
def peek(self): """ ------------------------------------------------------- Returns a copy of the value at the top of the stack. Attempting to peek at an empty stack throws an exception. Use: value = s.peek() ------------------------------------------------------- Returns: value - a copy of the value at the top of the stack (?) ------------------------------------------------------- """ assert len(self._values) > 0, "Cannot peek at an empty stack" value = deepcopy(self._values[-1])
return value # Your code here
def __iter__(self): """ FOR TESTING ONLY ------------------------------------------------------- Generates a Python iterator. Iterates through the stack from top to bottom. Use: for v in s: ------------------------------------------------------- Returns: value - the next value in the stack (?) ------------------------------------------------------- """ for value in self._values[::-1]: yield value
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
