Question: Class Stack_array code: class Stack: def __init__(self): ------------------------------------------------------- Initializes an is_empty stack. Data is stored in a Python list. Use: s = Stack() -------------------------------------------------------
Class Stack_array code:
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
Question!!
# Imports from enum import Enum # Enumerated constant MIRRORED = Enum('MIRRORED', {'IS_MIRRORED': "is a mirror", 'TOO_MANY_LEFT': "too many characters in L", 'TOO_MANY_RIGHT': "too many characters in R", 'MISMATCHED': "L and R don't match", 'INVALID_CHAR': "invalid character", 'NOT_MIRRORED': "no mirror character"}) def is_mirror_stack(string, valid_chars, m): """ ------------------------------------------------------- Determines if string is a mirror of characters in valid_chars around the pivot m. A mirror is of the form LmR, where L is the reverse of R, and L and R contain only characters in valid_chars. Use: mirror = is_mirror_stack(string, valid_chars, m) ------------------------------------------------------- Parameters: string - a string (str) valid_chars - a string of valid characters (str) m - the mirror pivot string (str - one character not in valid_chars) Returns: mirror - the state of the string (Enum MIRRORED) ------------------------------------------------------- """ assert m not in valid_chars, \ "cannot use '{}' as the mirror character".format(m)


![------------------------------------------------------- Returns: a new Stack object (Stack) ------------------------------------------------------- """ self._values = []](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f52d00d76b3_87266f52d004176e.jpg)
This function must use a Stack to determine if string is a mirror. See the notes in CP164 Notes: Stacks - The Mirror Problem. Note that this version is somewhat more sophisticated that the version in the notes in that it returns a result that tells you, if the string is not a mirror, why it failed. Those enumerated constant results are given above the function description. For example, if the string parameter is a valid mirror string, then the return value mirror should be assigned the result MIRRORED. IS_MIRRORED: mirror = MIRRORED.IS_MIRRORED Some examples: The following: mirror = mirror_stack("cmc", "abc", "m") mirror = is_mirror_stack("zzxzuzxzz", "xyz", "W"). mirror - is_mirror_stack("_tot ".. "tot"> should all return MIRRORED.IS_MIRRORED. P. mirror = is mirror stack("cmc", "ab", "m") should return MIRRORED.INVALID_CHAR. -------------------------------------------------------- mirror = is_mirror_stack("zzxzxzxzz", "xyz', "u") --- should return XIRRORED.NOT_MIRRORED. mirror = is_mirror_atack("zzxzuzx", "xyz', "u") should return MIRRORED. TOO_MANY_LEFT. Each enumerated constant in MIRRORED has two parts: the name (ex: INVALID_CHAR) and the value (ex: invalid character). Your testing program can print a more readable result for an enumerated constant by simply printing the value attribute of the enumerated constant : mirror = is_mirror_stack ("cmc", "abc", "m) print("Mirror: ( 1.format(mirror.value)) prints: Mirror: is a mirror CP164: Notes - The Stack: The Mirror Problem mirror can be thought of as a special forn We have already used stacks check if brackets are properly balanced. We can use a similar idea to determine if a string is a mirror. of a palindrome, and consists of a string of the form: LOR where: m is the mirror character at the centre of the string L and R are strings where R is the reverse of L (They may be empty strings) L and R are formed from a limited set of characters that do not include m Some examples of the "abc" mirror pattern (i.e. L and R are limited to the characters "abc"): cmc bba amaabb M abacmcaba -------- These strings do not fit the "abc" mirror pattern: abmb gmg abacmcabb Write and test the following function: def is_mirror(string): Determines if a string is a mirror string or not. (Uses a stack) Use: b - is mirror (string) Parameters: string - the string to test (str) Returns: mirror - True if string is a mirror, False otherwise (boolean) ------- def is_mirror(string): Determines if a string is a mirror string or not. (Uses a stack) Use: b = is_mirror(string) Parameters: string the string to test (str) Returns: mirror - True if string is a mirror, False otherwise (boolean) mirror = True stack = Stack() n = len(string) i = 0 while mirror and i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
