Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3 defining a Class - Lots of screenshots uploaded for reference/background info Q2.1: Implement the Queue You will need the following instance attributes: front

Python 3 defining a Class - Lots of screenshots uploaded for reference/background info

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Q2.1: Implement the Queue You will need the following instance attributes: front - index of the front of the queue rear - index of the first empty slot (or index of the last element, it is up to you) num_elems integer that tracks the number of elements present in the queue capacity - capacity of the queue, initially 3, initialize your array with elements set to None data - an array of length capacity (represents your queue) To implement the behavior of the ADT you need to provide implementations for the following methods . dequeue - removes and returns the front element. Notice the asserts in the doctests! Do create an assert statement in this method. enqueue - adds element, doubling the capacity if is full .is_ful1 - checks whether a queue is full. Returns True if full, False otherwise. is_empty - checks whether a queue is empty. Returns True if empty, False otherwise. print_queue-prints queue. o Format: Assume that elements 4, 5, 6, 7, 8 were added to the queue q. Then q. print-queue() will print: [ I 4 I 5 I 6 I 7 I 8 I NOTE: There are spaces in between the | and the numbers in q.print_queue ). If you ignore these spaces, you will lose some points o If a queue is empty, print: [ ] Define class Queue that implements the ADT (Abstract Data Type) Queue using a circular array. . You must use a np.array for this question: o Somewhere in your constructor you must have a line that looks like self.data - o If you use a Python list, then your solution will get a 0 You must observe the FIFO convention. When implementing a queue, we have to worry about running time for its basic operations: dequeue and enqueue. Both of them must run in Theta(1) Unfortunately, a regular array does not give you desired complexity. Let me explain why: We will assume that front (index) points to the front of a queue, and rear (index) points to the end of a queue, to the next available location. Front always stays at 0, and we move rear when we add elements. AssertionError: attempt to dequeue from an empty queue >b.enqueue (1) >b.enqueue (max) >>b.print_queue() [ 1 | ] b.dequeue() 1 >b.dequeue() >b.front >>>b. rear >b.dequeue() Traceback (most recent call last): AssertionError: attempt to dequeue from an empty queue def init_(self,capacity 3): :param capacity: a positive integer qQueue) # Your Code Here def dequeue(self) dequeues from the front of the queue >q.dequeue() Traceback (most recent call last): AssertionError: attempt to dequeue from an empty queue def enqueue (self,elem): enqueue at the rear of the queue :param elem: a value that is not None >q.enqueue ("a") # Your Code Here def expand(self) expand the capacity of the circular array when needed >qQueue () >q.capacity >>>q.expand() >>q.capacity # Your Code Here def is full(self): checks if circu lar array is fut >Queue () >>>for i in range(4): q.enqueue(i) >>q.data array ([0, 1, 2, 3, None, None], dtype-object) >>> q.is_full() False # Your Code Here def is_empty(self) checkS 1T circular array 1s futl >>> q = Queue() True # Your Code Here q.is_empty() def print_queue(self) prints out queue in a human-friend ly format >qQueue() >for i in range (5): q.enqueue(i) >>> q.print_queue() [|01 |2|341 >pQueue() >>> p.print_queue() # Your Code Here

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

Students also viewed these Databases questions

Question

define aggression,

Answered: 1 week ago

Question

Write down the Limitation of Beer - Lamberts law?

Answered: 1 week ago

Question

Discuss the Hawthorne experiments in detail

Answered: 1 week ago

Question

Explain the characteristics of a good system of control

Answered: 1 week ago

Question

State the importance of control

Answered: 1 week ago