0% found this document useful (0 votes)
17 views

DSA Homework Assignment Week6 Queue

This document contains instructions for a homework assignment on queues and related data structures. Students are asked to implement a queue with limited capacity, use stacks to implement queues and vice versa, and write a function to calculate the time it takes for a person in a ticket line to finish buying tickets using a queue representation of the line. The document provides sample code for a basic Stack and Queue class to use in the problems and examples of the expected output.

Uploaded by

Tuan Anh Tran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

DSA Homework Assignment Week6 Queue

This document contains instructions for a homework assignment on queues and related data structures. Students are asked to implement a queue with limited capacity, use stacks to implement queues and vice versa, and write a function to calculate the time it takes for a person in a ticket line to finish buying tickets using a queue representation of the line. The document provides sample code for a basic Stack and Queue class to use in the problems and examples of the expected output.

Uploaded by

Tuan Anh Tran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Faculty of Mathematical Economics

Data Structures and Algorithms

Instructor: Nguyen Thanh Tuan


DSEB Class of 2021 - 2024

Homework Assignment Week 6


Topic: Queue
Date Created: February 23, 2023

Problem 1: Queue Implementation With Limited Capacity


a. Implement the Queue class that you learned in the class again with below structure:

1 class Queue :
2 """ FIFO Queue implementation using a Python list as underlying
storage . """
3
4 def __init__ ( self ) :
5 """ Create an empty queue """
6 pass
7
8 def __len__ ( self ) :
9 """ Return the number of elements in the queue """
10 pass
11

12 def is_empty ( self ) :


13 """ Return True if the queue is empty """
14 pass
15
16 def __repr__ ( self ) :
17 """ Return string representation of the queue """
18 pass
19
20 def first ( self ) :
21 """ Return ( but do not remove ) the element at the front of
22 the queue .
23

24 Raise Empty exception if the queue is empty .


25 """
26 pass
27
28 def dequeue ( self ) :
29 """ Remove and return the first element of the queue .
30
31 Raise Empty exception if the queue is empty .
32 """
33 pass
Data Structures and Algorithms Homework Week 6

34

35 def enqueue ( self , e ) :


36 """ Add an element to the back of queue """
37 pass

b. Modify the Queue implementation above so that the queue’s capacity is limited to
maxlen elements, where maxlen is an optional parameter to the constructor (that defaults
to None). If enqueue is called when the queue is at full capacity, throw a Full exception
(defined similarly to Empty).

1 class QueueMaxLen :
2 """ FIFO Queue implementation with limited capacity using a Python
list as underlying storage . """
3

4 # your code here

Problem 2: QueueByStack and StackByQueue


a. Using Stack from previous class (or using class Stack below) to implement Queue:

1 class Stack :
2 """ LIFO Stack implementation using a Python list as underlying storage
"""
3
4 def __init__ ( self ) :
5 """ Create an empty stack """
6 self . _data = [] # nonpublic list instance
7
8 def __len__ ( self ) :
9 """ Return the number of elements in the stack . """
10 return len ( self . _data )
11
12 def is_empty ( self ) :
13 """ Return True if the stack is empty . """
14 return len ( self . _data ) == 0
15
16 def push ( self , e ) :
17 """ Add element e to the top of the stack . """
18 self . _data . append ( e ) # new item stored at end of list
19
20 def top ( self ) :
21 """ Return ( but do not remove ) the element at the top of the stack .
22
23 Raise Empty exception if the stack is empty .
24 """
25 if self . is_empty () :
26 raise Empty ( " Stack is empty " )
27 return self . _data [ -1] # the last item in the list
28
29 def pop ( self ) :
30 """ Remove and return the element from the top of the stack ( i . e . ,
LIFO ) .

2
Data Structures and Algorithms Homework Week 6

31

32 Raise Empty exception if the stack is empty .


33 """
34 if self . is_empty () :
35 raise Empty ( " Stack is empty " )
36 return self . _data . pop () # remove last item from list

b. Using Queue from Problem 1.a. to implement Stack.

Problem 3: Buy Tickets for Blackpink Concert


There are n people in a line queuing to buy tickets for Blackpink concert, where the 0th
person is at the front of the line and the (n − 1)th person is at the back of the line.

You are given a 0-indexed integer array tickets of length n where the number of tickets
that the ith person would like to buy is tickets[i].

Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a
time and has to go back to the end of the line (which happens instantaneously) in order
to buy more tickets. If a person does not have any tickets left to buy, the person will
leave the line.

Implement a function using Queue to return the time taken for the person at position k
(0-indexed) to finish buying tickets.

Note: To get full mark at this problem, you had better use tickets as Queue

See the examples below:

1 tickets = [2 ,3 ,2]
2 k = 2
3 get_time ( tickets , k )
4 >>> 6
5 """ Explanation :
6 - In the first pass , everyone in the line buys a ticket and the line
becomes [1 , 2 , 1].
7 - In the second pass , everyone in the line buys a ticket and the line
becomes [0 , 1 , 0].
8 The person at position 2 has successfully bought 2 tickets and it took
3 + 3 = 6 seconds .
9 """
10
11 tickets = [5 ,1 ,1 ,1]
12 k = 0
13 get_time ( tickets , k )
14 >>> 8
15 """ Explanation :
16 - In the first pass , everyone in the line buys a ticket and the line
becomes [4 , 0 , 0 , 0].

3
Data Structures and Algorithms Homework Week 6

17 - In the next 4 passes , only the person in position 0 is buying tickets


.
18 The person at position 0 has successfully bought 5 tickets and it took
4 + 1 + 1 + 1 + 1 = 8 seconds . """

Guidelines for submission

• Your submission must be under the .ipynb format.

• Your submission will be graded and it is likely that homework grade will contribute
as a component in your GPA.

• If your submission is later than the due date without special consideration approval,
you will receive a penalty on your mark.

You might also like