Question

PYTHON please help this is very confusing! THANK YOU!Q5 18 Points In this question, we will implement the DrippingStack class. This is a fixed capacity stack; its size does nottop(self): returns the element at top of the DrippingStack without removing it from the DrippingStack. (LIFO ordering). If Dr

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

from queue import Empty


# DrippingStack class
class DrippingStack:
    # constructor
    def __init__(self, capacity):
        # ensuring that capacity is valid
        assert capacity > 0
        # creating a fixed array (list) of capacity number of elements
        self.__array = [None] * capacity
        # setting 0 as bottom index as well as the count
        self.__bottom = 0
        self.__count = 0

    def push(self, item):
        # adding count to bottom and wrapping around if necessary to get the index of next position
        # to add new value
        index = (self.__bottom + self.__count) % len(self.__array)
        # adding item to index position. if the array is full, the oldest element will be automatically
        # overwritten by new item
        self.__array[index] = item
        # checking if array is currently full (before adding new element)
        if self.__count == len(self.__array):
            # advancing bottom index, and wrapping around if necessary
            self.__bottom = (self.__bottom + 1) % len(self.__array)
        # otherwise incrementing count
        else:
            self.__count += 1

    def pop(self):
        # if empty, throwing exception
        if self.__count == 0:
            raise Empty()
        # otherwise finding index of top element by adding count-1 to bottom, ( wrapping around if necessary)
        index = (self.__bottom + self.__count - 1) % len(self.__array)
        # storing value
        value = self.__array[index]
        # changing value to None
        self.__array[index] = None
        # decrementing count
        self.__count -= 1
        # returning value
        return value

    def top(self):
        if self.__count == 0:
            raise Empty()
        # finding index of top element by adding count-1 to bottom, ( wrapping around if necessary)
        index = (self.__bottom + self.__count - 1) % len(self.__array)
        # returning value at index, without removing
        return self.__array[index]

    def is_empty(self):
        return self.__count == 0

    def __len__(self):
        return self.__count


# end of DrippingStack class


# code for testing, remove if not needed
s1 = DrippingStack(5)
s1.push(4)
s1.push(5)
s1.push(10)
s1.push(15)
print(s1.pop())  # 15
s1.push(200)
s1.push(500)
s1.push(19)
print(len(s1))  # 5
print(s1.pop())  # 19

#output

15

5

19

Add a comment
Know the answer?
Add Answer to:
PYTHON please help this is very confusing! THANK YOU! Q5 18 Points In this question, we...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON - Write and test the following program that meets the following conditions ------------------------------------------------------- write a...

    PYTHON - Write and test the following program that meets the following conditions ------------------------------------------------------- write a program that Tests the methods of stack for empty and non-empty stacks using the data in is_empty, push, pop, peek *Testing pop and peek while empty throws exception ------------------------------------------------------- CONDITIONS source - list of data (list of ?) RETURNS None ------------------------------------------------------- CODE FOR : Empty, Push, Pop & Peek def is_empty(self):    result = True if len(self._values) > 0: result = False return result...

  • 11.2 Problem Set 2: PostScript Stack Commands (in python please) For this assignment you are to...

    11.2 Problem Set 2: PostScript Stack Commands (in python please) For this assignment you are to implement a PostScript command interpreter using a Stack. You must implement the stack using a singly-linked list. Solutions that use a data structure other than a singly-linked list will received no credit. The interpreter must read and execute a string of PostScript commands based on the following definitions: 1. = objn objn-1 … obj1 = : objn-1 … obj1 This command removes the topmost...

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These...

    In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT