Hi, I would like the following python code rewritten in adifferent way/structure etc. I got it from a question I posted butam worried fellow classmates will also be using it so am coveringbases. her it is.
#threeUniqueSongs.py
#Simulation of professor listening to 3 songs out of playlist of4 songs 10000 times sampling with replacement possible
import random
import math
#Here playlist is the list of 4 songs i.e. \"Eastside\", \"BetterNow\", \"Lucid Dreams\", \"Harder, Better, Faster, Stronger\"
def chooseSong(playlist):
#randomly choose a song out of the playlist with repition possibleusing 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 startsfrom 0
#so last song has index len(playlist)-1
return playlist[songIndex] #select a song out of the 4 songs atrandom
 Â
def chooseThreeSongs(iteration, playlist):
songsChosen = [] #to store the songs chosen in the currentiteration
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 simulationswith 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 forlistening
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 thatafter 3 songs are played that your professor listens to threeunique songs is %s .\" %probThreeUniqueSongs)
 Â
averageUniqueSongsListened =math.ceil(totalUniqueSongsListened/(3*simulations)) #as ineach
#simulation 3 songs are listened
print(\"After \"+str(simulations)+\" simulations, the average numberof unique songs that your professor listens to is %s .\"%averageUniqueSongsListened)
 Â
 Â
#Invoking main() function
if __name__ == \"__main__\":