DSA Homework Assignment Week6 Queue
DSA Homework Assignment Week6 Queue
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
34
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
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
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
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
• 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.