Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I need help with Python, please. I am running into some errors that I can't figure out. Here are some of my Stacks right

Hi, I need help with Python, please. I am running into some errors that I can't figure out.

image text in transcribed

image text in transcribed

Here are some of my Stacks right now:

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 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 len(self._values) == 0, "Cannot pop from an empty stack"

value = deepcopy(self._values[self._top])

self._values.pop()

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

Thanks so much!

Test for assertion fails: 'peek' ERROR: 'Stack' object has no attribute 'values' Test 'peek' with various values: '[99]' ERROR: 'Stack' object has no attribute 'values' Test 'peek' with various values: '[11, 22, 33, 44, 55, 66]' ERROR: 'Stack' object has no attribute 'values' Test 'pop' with various values: '[99]' ERROR: Remaining stack incorrect Test 'pop' with various values: '[1,1,1, 1, 1, 1]' ERROR: Remaining stack incorrect

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions