Question

Exceptions are raised when preconditions are violated. For example, an IndexError will be raised if a...

Exceptions are raised when preconditions are violated. For example, an IndexError will be raised if a program attempts to dequeue from an empty queue. You should modify the dequeue() method and peek() method of the Queue class to raise an IndexError with the message "ERROR: The queue is empty!" if an attempt is made to dequeue from an empty queue or peek into an empty queue.

Submit the entire Queue class definition in your answer to this question.

Test Result
try:
    q = Queue()
    q.enqueue(2)
    print(q.dequeue())
except IndexError as err:
    print (err)
2
try:
    q = Queue()
    q.enqueue(2)
    q.enqueue(1)
    print(q.dequeue())
    print(q.dequeue())
    print(q.dequeue())
except IndexError as err:
    print (err)
2
1
ERROR: The queue is empty!
try:
    q = Queue()
    print(q.peek())
except IndexError as err:
    print (err)
ERROR: The queue is empty!

Homework Answers

Answer #1

# Thank you for asking the question.

class Queue:
    def __init__(self):
        self.elements = []
    def enqueue(self, item):
        self.elements.insert(0,item)

    def dequeue(self):
        try:
            return self.elements.pop()
        except IndexError as err:
            return "ERROR:The queue is empty!"
    def peek(self):
        try:
            return self.elements[len(self.elements)-1]
        except IndexError as err:
            return "ERROR:The queue is empty!"
q=Queue()
q.enqueue(4)
q.enqueue(5)
q.enqueue(6)
print(q.dequeue())
print(q.dequeue())
print(q.dequeue())
print(q.dequeue())
q.enqueue(10)
print(q.dequeue())
print(q.peek())

# Hope this will help you.

# Feel free to ask any doubt and once again thank you very much.

Know the answer?
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
Similar Questions
In this project you implement a program such that it simulates the process of repeated attempts...
In this project you implement a program such that it simulates the process of repeated attempts to hit a target with a projectile. The goal is to shoot the projectile within a 1 foot distance from the target, since such a short miss is accepted as a hit. Your program is responsible for the following tasks. Compute the trajectory data of a projectile (such as the time, the maximum height and the distance as described by the formulas above) for...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these classes: Product, Customer, and Store. All data members of each class should be marked as **private** (a leading underscore in the name). Since they're private, if you need to access them from outside the class, you should do so via get or set methods. Any get or set methods should be named per the usual convention ("get_" or "set_" followed by the name of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT