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!! def reroute(opstring, values_in): """ ------------------------------------------------------- Reroutes values in a list according to a operating string and returns a new list of values. values_in is unchanged. In opstring, 'S' means push onto stack, 'X' means pop from stack into values_out. Use: values_out = reroute(opstring, values_in) ------------------------------------------------------- Parameters: opstring - String containing only 'S' and 'X's (str) values_in - A valid list (list of ?) Returns: values_out - if opstring is valid then values_out contains a reordered version of values_in, otherwise returns None (list of ?) ------------------------------------------------------- """

Consider the following diagram which depicts a railway switching network. Imagine train cars arriving from the right, driving into the stack, and leaving to the left. Cars Out Cars In X (pop) s (push) Stack Railcar Rerouting Imagine four railroad cars positioned on the input side of the track in the figure above, numbered 1, 2, 3, and 4, respectively. Suppose we perform the following sequence of operations (which is compatible with the direction of the arrows in the diagram and does not require cars to "jump over" other cars): 1. move car 1 into the stack; 2. move car 2 into the stack; 3. move car 2 into the output; 4. move car 3 into the stack; 5. move car 4 into the stack; 6. move car 4 into the output; 7. move car 3 into the output; 8. move car 1 into the output. As a result of these operations the original order of the cars, 1234, has been changed into 2431. The operations above can be more concisely described by the code SSXSSXXX, where S stands for "move a car from the input into the stack", and X stands for "move a car from the stack into the output". Some sequences of Ss and X's specify meaningless operations, since there may be no cars available on the specified track; for example, the sequence SXXSSXXS cannot be carried out. (Try it to see why.) The function must use one and only one stack to solve the
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
