1. Engineering
  2. Computer Science
  3. hello recently got this from another zookal question im getting...

Question: hello recently got this from another zookal question im getting...

Question details

Hello, recently got this from another Zookal question. Im getting indent errors and know nothing about python, was wondering if you can give the following code back with appropriate indentations. Im running it on spyder for python. Thanks

Ans. Hope the following python code helps to solve the problem.

#threeUniqueSongs.py

#Simulation of professor listening to 3 songs out of playlist of 4 songs 10000 times sampling with replacement possible
import random
import math

#Here playlist is the list of 4 songs i.e. "Eastside", "Better Now", "Lucid Dreams", "Harder, Better, Faster, Stronger"
def chooseSong(playlist):
#randomly choose a song out of the playlist with repition possible using random.randint function
songIndex = random.randint(0, len(playlist)-1) #len(playlist) = Size of playlist list here 4,
#here we consider from 0 to len(playlist)-1 as list indexing starts from 0
#so last song has index len(playlist)-1
return playlist[songIndex] #select a song out of the 4 songs at random
  
def chooseThreeSongs(iteration, playlist):
songsChosen = [] #to store the songs chosen in the current iteration
firstSong = chooseSong(playlist)
songsChosen.append(firstSong)
  
secondSong = chooseSong(playlist)
songsChosen.append(secondSong)
  
thirdSong = chooseSong(playlist)
songsChosen.append(thirdSong)
#to print the three songs chosen in comma separated manner
print("For iteration "+str(iteration)+" songs choosen are: "+str(firstSong), str(secondSong), str(thirdSong), sep=',')
return songsChosen

def findUniqueSongs(songsChosen):
#to find number of unique songs out of the 3 songs chosen
uniqueSongs = set() #set to store all unique songs say the song "Better Now" is repeated
#twice then uniqueSongs stores it only once by set property

for song in songsChosen:
uniqueSongs.add(song)
  
return len(uniqueSongs) #number of unique songs listened
  
#main function handling the simulation upto 10000 times
def main():
simulations = 10000
simulationWithThreeUniqueSongs = 0 #to count number of simulations with 3 unique songs
probThreeUniqueSongs = 0
totalUniqueSongsListened = 0
averageUniqueSongsListened = 0
  
#playlist of 4 songs as specified in the question
playlist = ["Eastside", "Better Now", "Lucid Dreams", "Harder, Better, Faster, Stronger"]
for simulation in range(1, simulations+1):
#to choose three songs out of the given playlist above for listening
songsChosen = chooseThreeSongs(simulation, playlist)
uniqueSongs = findUniqueSongs(songsChosen)
totalUniqueSongsListened += uniqueSongs
if(uniqueSongs == 3): #all 3 songs are unique
simulationWithThreeUniqueSongs += 1
  
probThreeUniqueSongs = simulationWithThreeUniqueSongs/simulations #basic probability formula
print("After "+str(simulations)+" simulations, the probability that after 3 songs are played that your professor listens to three unique songs is %s ." %probThreeUniqueSongs)
  
averageUniqueSongsListened = math.ceil(totalUniqueSongsListened/(3*simulations)) #as in each
#simulation 3 songs are listened
print("After "+str(simulations)+" simulations, the average number of unique songs that your professor listens to is %s ." %averageUniqueSongsListened)
  
  
#Invoking main() function
if __name__ == "__main__":
main()

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution