Question: I need help with Python for these 3 tasks, please. :) def pythonlist_to_Stack(stack, source): ------------------------------------------------------- Pushes contents of source onto stack. At finish, source
I need help with Python for these 3 tasks, please. :)
def pythonlist_to_Stack(stack, source):
"""
-------------------------------------------------------
Pushes contents of source onto stack. At finish, source is empty.
Last value in source is at bottom of stack,
first value in source is on top of stack.
Use: pythonlist_to_stack(stack, source)
-------------------------------------------------------
Parameters:
stack - a Stack object (Stack)
source - a Python list (list)
Returns:
None
-------------------------------------------------------
"""
# your code here
def stack_to_pythonlist(stack, target):
"""
-------------------------------------------------------
Pops contents of stack into target. At finish, stack is empty.
Top value of stack is at end of target,
bottom value of stack is at beginning of target.
Use: stack_to_pythonlist(stack, target)
-------------------------------------------------------
Parameters:
stack - a Stack object (Stack)
target - a Python list (list)
Returns:
None
-------------------------------------------------------
"""
# your code here
def test_Stack(source):
"""
-------------------------------------------------------
Tests the methods of Stack for empty and
non-empty stacks using the data in source:
is_empty, push, pop, peek
(Testing pop and peek while empty throws exceptions)
Use: stack_test(source)
-------------------------------------------------------
Parameters:
source - list of data (list of ?)
Returns:
None
-------------------------------------------------------
"""
# your code here
Here is what goes through the testing file:
Test 'pythonlist_to_Stack' with python list: '[]'
Test 'pythonlist_to_Stack' with python list: '[99]'
Test 'pythonlist_to_Stack' with python list: '[11, 22, 33, 44, 55]'
Test 'pythonlist_to_Stack' with python list: '[55, 44, 33, 22, 11]'
Test 'Stack_to_pythonlist' with python Stack: '[]'
Test 'Stack_to_pythonlist' with python Stack: '[99]'
Test 'Stack_to_pythonlist' with python Stack: '[11, 22, 33, 44, 55]'
Test 'Stack_to_pythonlist' with python Stack: '[55, 44, 33, 22, 11]'
Thank you so much!
Stacks:
In the Stacks_array module:
DEFAULT_SIZE = 10
def __init__(self, max_size = DEFAULT_SIZE): """ ------------------------------------------------------- Initializes an empty stack. Data is stored in a fixed-size Python list. Use: stack = Stack(max_size) Use: stack = Stack() # uses default max size ------------------------------------------------------- Parameters: max_size - maximum size of the Stack (int > 0) Returns: a new Stack object (Stack) ------------------------------------------------------- """ assert max_size > 0, "Queue size must be > 0" self._capacity = max_size self._values = [None] * self._capacity self._top = -1
def isEmpty(self): """ ------------------------------------------------------- Determines if the stack is empty. Use: b = stack.is_empty() ------------------------------------------------------- Returns: True if stack is empty, False otherwise ------------------------------------------------------- """ # your code goes here if self._top == -1: empty = True else: empty = False return empty
def isFull(self): """ ------------------------------------------------------- Determines if the stack is full. Use: b = stack.isFull() ------------------------------------------------------- Returns: True if stack is full, False otherwise ------------------------------------------------------- """ # your code goes here if self._top == self._capacity -1: full = True else: full = False return full
def push(self, element): """ ------------------------------------------------------- Pushes a copy of the given element onto the top of the stack. Use: stack.push(element) ------------------------------------------------------- Parameters: element - the data element to be added to stack (?) Returns: None ------------------------------------------------------- """ assert not self.isFull(), "Stack is full" self._top += 1 self._values[ self._top ] = deepcopy( element ) return
def pop(self): """ ------------------------------------------------------- Pops and returns the top of stack. The data element is removed from the stack. Attempting to pop from an empty stack throws an exception. Use: value = stack.pop() ------------------------------------------------------- Returns: value - the value at the top of stack (?) ------------------------------------------------------- """ assert not self.isEmpty(), "Cannot pop from an empty stack"
value = deepcopy( self._values[ self._top ] ) self._top -=1 return value
def peek(self): """ ------------------------------------------------------- Returns a copy of the value at the top of the stack without removing it. Attempting to peek at an empty stack throws an exception. Use: value = stack.peek() ------------------------------------------------------- Returns: value - a copy of the value at the top of stack (?) ------------------------------------------------------- """ #your code goes here assert len(self._values) > 0, "Cannot peak at an empty stack" value = deepcopy(self.values[-1]) return value
Step by Step Solution
There are 3 Steps involved in it
To address your Python tasks we need to implement several functions that manipulate stacks and lists as well as test these functionalities Lets break down each requirement step by step Task 1 Convert ... View full answer
Get step-by-step solutions from verified subject matter experts
