Question: Write and test the following function that uses a Stack: def stack_split_alt(source): ------------------------------------------------------- Splits the source stack into separate target stacks. When finished source
Write and test the following function that uses a Stack:
def stack_split_alt(source): """ ------------------------------------------------------- Splits the source stack into separate target stacks. When finished source stack is empty. Values are pushed alternately onto the returned target stacks. Use: target1, target2 = stack_split_alt(source) ------------------------------------------------------- Parameters: source - the stack to split into two parts (Stack) Returns: target1 - contains alternating values from source (Stack) target2 - contains other alternating values from source (Stack) ------------------------------------------------------- """
Add the function to a PyDev module named functions.py. Test it from .
This function uses a stack, meaning you may manipulate the stack using only the stack interface methods: push, pop, is_empty, and peek. You may not use or refer to the internal Stack elements such as _values.
Sample result (values are listed top to bottom):
| source | target1 | target2 |
|---|---|---|
8 14 12 9 8 7 5 | 5 8 12 8 | 7 9 14 |
here are the Class Stack 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
