Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Extend the Stack class by adding the method_ str (self). It is used to print out

ID: 3747528 • Letter: E

Question

Extend the Stack class by adding the method_ str (self). It is used to print out all items in the stacks. Note, this method does not remove any elements from the stack. You should include the entire Stack class definition. For example 2 Extend the Stack class by addling the method push Jistiself,my. list) that push all elements from the ist into the stack one by one Test Result You should include the entire Stack class definition in your answer to this question. my stack-Stack() Stack my stack-push('a') my_stack.push( 'b) my_stack.push('c) print (my_stack) [a, b, c] For example: Test Result my_stack Stack) Stack: [] my_stack.push'a) my stack-push('b') my_stack.pop) my_stack.pop) print (my_stack) my-stack Stack() rylist-[4,5,6] my_stack.push_list(mylist) print (my_stack.peek)) print(my_stack.size)) tend the Stack class by addng the method multi pop(self, num) that pops "nu" of elements from the stack or until the stack s empty there is ot enough iems inside the stack. This method should not pop out any tem and reurn None. It returns a list of elements if the items are popped out You should include the entise Stack class defnition in your answer to this question For example: my_stack Stack() my_stack.push_list(['a']) print(my stack.peek()) print(my_stack.size()) Test Result y stack-Stack) ny.stack.pushc 'a) aystack-push('b') mystack.push(') print(ay stack.peek)) print(ey stack.multi pop(3)) print(ay stack,size)) y stack Stack) ay stack.push'd') my stack.push'e') ay stack-push print(ey stack.multi pop(5)) print(ay stack.size)) print(ey stack.pesk))

Explanation / Answer

It's unclear from the question whether a psuedo-code is required or the code itself.

So, I'm gonna go ahead and mention the psuedocodein here cause I don't know the definition of Stack(not available in the question) and the language for writing the code is not mentioned too.

So, I'll write the algorithm and the psuedo_code accordingly :)

1. To solve this part which says that you need to print the entire stack without modifying it, I would recommend the usage of a second stack.

Let's say stack_1 is the original stack and stack_2 is an empty stack.

Algorithm:

This will make sure you have printed/displayed all the elements in the stack_1 and you have the reverese of the original stack in stack_2.

To get back to the original stack:

This will make sure you have your stack back in order in stack_1.

2. Let's define a method push_list() as mentioned above.

Now, the purpose of this method is to take elements one-by-one from an array and push it to the stack.

Let's say we have a stack called cool_stack :D

We need to push elements to it from a list. Let's see the psudecode for that.

```
     for element in list:

         cool_stack.push(element)

```

This would do the second part of the problem.

3. This part requires a multi_pop() function to be implemented as the Stack member function.

So, this again is a repetitive process of popping out elements from the stack.

We can say this is the opposite of the 2nd part.

Let's say we define the multipop_function.

```
       def multi_pop(self, num):

            if cool_stack.size()<num:

                 return 0

           else:

                 for i in range(num):

                     cool_stack.pop()

                 return 1

NOTE: The function above takes the liberty of returning a 1 for successful execution and a 0 otherwise coz nothing specific was mentioned in the question.

So, this answers all the three parts of the problem.

Hope this helps :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote