Question: import heapq ' ' ' You are working for a promising new music streaming service Algo - fy . Algo - fy would like to
import heapq
You are working for a promising new music streaming service Algofy
Algofy would like to offer a new ranking feature that will track the top k most streamed songs of the day.
Youve been tasked with building a class AlgoFy that can track the most streamed songs
and return an accurate list top k list at any point in time.
Another team has already developed the song streaming feature: iStream and you will need to integrate with it
iStream will send AlgoFy play data as it processes customer requests.
This data will be batched so the AlgoFy class will need to ingest lists of played songs multiple times
A song is identified with an integer id number.
The AlgoFy class will need to have the following methods:
Constructork: the number of songs to track in the topk
streamSongsList of songIds: Receive a batch of streamed songs updates internal state
getTopK: return the top k most streamed songs in order
As an example, the AlgoFy class may see the following usage:
ranker new AlgoFy
ranker.streamSongs
ranker.streamSongs
ranker.getTopK
ranker.streamSongs
ranker.getTopK
You must solve this problem by effectively using the properties of a Priority Queue or Heap.
You must also provide an explanation of the running time of both getTopK and streamSongs
Code Author:
Running Time Analysis of gettopk
Running Time Analysis of streamsongs
class AlgoFy:
def initself k:
self.k k
def streamsongsself songIds:
pass
def gettopkself:
return
DO NOT EDIT BELOW THIS
Below is the unit testing suite for this file.
It provides all the tests that your code must pass to get full credit.
class TestAlgoFy:
def rununittestsself:
self.testexample
self.testexample
self.testmanybatches
self.testfewerthank
self.testempty
The test below will slow down your code if the runtime is inefficient.
Save this test for last. Once you have somewhat finalized code, uncomment this
test case and see if your code still runs quickly.
If it runs too slowly, there's a chance the autograder on Gradescope will
time out and not be able to grade your code within the allotted time.
self.testverylargek
def printtestresultself testname, result:
color m if result else m
reset m
printfcolorresulttestnamereset
def testanswerself testname, result, expected:
if result expected:
self.printtestresulttestname, True
else:
self.printtestresulttestname, False
printfExpected: expected
Got: result
def testexampleself:
ranker AlgoFy
ranker.streamsongs
ranker.streamsongs
result ranker.gettopk
expectedanswer
self.testanswertestexample", result, expectedanswer
def testexampleself:
ranker AlgoFy
ranker.streamsongs
ranker.streamsongs
ranker.gettopk
ranker.streamsongs
result ranker.gettopk
expectedanswer
self.testanswertestexample result, expectedanswer
def testmanybatchesself:
ranker AlgoFy
for i in range:
for j in range:
ranker.streamsongsi j
ranker.gettopk
ranker.streamsongs
ranker.streamsongs
result ranker.gettopk
expectedanswer
self.testanswertestmanybatches", result, expectedanswer
def testfewerthankself:
ranker AlgoFy
ranker.streamsongs
result ranker.gettopk
expectedanswer
self.testanswertestfewerthank result, expectedanswer
def testemptyself:
ranker AlgoFy
result ranker.gettopk
expectedanswer
self.testanswertestempty", result, expectedanswer
def testverylargekself:
ranker AlgoFy
for i in range:
for j in range:
for z in rangei:
ranker.streamsongsi
result ranker.gettopk
expectedanswer listrange
self.testanswertestverylargek result, expectedanswer
if namemain:
testrunner TestAlgoFy
testrunner.rununittests
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
