Question
Write and test the following function that uses a Stack: def stack_combine(source1, source2): ------------------------------------------------------- Combines two source stacks into a target stack. When finished,
Write and test the following function that uses a Stack: def stack_combine(source1, source2): """ ------------------------------------------------------- Combines two source stacks into a target stack. When finished, the contents of source1 and source2 are interlaced into target and source1 and source2 are empty. Use: target = stack_combine(source1, source2) ------------------------------------------------------- Parameters: source1 - a stack (Stack) source2 - another stack (Stack) Returns: target - the contents of the source1 and source2 are interlaced into target (Stack) ------------------------------------------------------- """
This function uses a stack, meaning you may manipulate the stack using only the stack interface methods: push, pop, is_empty, and peek.
Sample result (values are listed top to bottom):
source1 | source2 | target |
---|---|---|
5 8 12 8 | 3 6 1 7 9 14 | 14 9 7 8 1 12 6 8 3 5 |
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started