In: Computer Science
Hi, I would like the following python code rewritten in a different way/structure etc. I got it from a question I posted but am worried fellow classmates will also be using it so am covering bases. her it is.
#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__":
Changes done in structure made static "3" songs to dynamic number so that simulation can be found of any number ( For unique purpose )
#threeUniqueSongs.py
from __future__ import print_function
#Simulation of professor listening to 3 songs out of playlist of 4
songs 10000 times sampling with replacement possible
from random import randint
from math import ceil
#songPlaylist contains "Eastside", "Better Now", "Lucid
Dreams", "Harder - Better - Faster - Stronger"
def getRandomSong(songPlaylist):
#randomly choose a song from songPlaylist with repetion using
randint function
songIndex = randint(0,len(songPlaylist)-1) #choose random index
from 0 to songPlayList Count
return songPlaylist[songIndex] #select a song from list of
songPlaylist songs
def getSongs(iteration, playlist,numberUniqueSongs):
songsList=[]
songsChosen=[]
for randomSong in range(0,numberUniqueSongs):
songsList.append(getRandomSong(playlist))
songsChosen.append(songsList[len(songsList)-1])
songsListString = ','.join(songsList)
print("For iteration "+str(iteration)+" songs choosen are:
"+songsListString)
return songsChosen
def getUniqueSongs(songsChosen):
#to find number of unique songs
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
def simulateSongs():
simulations = 10000
numberUniqueSongs = 3
simulationWithUniqueSongs = 0 #to count number of simulations with
Songs
probUniqueSongs = 0
totalUniqueSongsListened = 0
averageUniqueSongsListened = 0
#songPlaylist of 4 songs as specified in the question
songPlaylist = ["Eastside", "Better Now", "Lucid Dreams", "Harder ,
Better , Faster , Stronger"]
for simulation in range(1, simulations+1):
songsChosen = getSongs(simulation,
songPlaylist,numberUniqueSongs)
uniqueSongs = getUniqueSongs(songsChosen)
totalUniqueSongsListened += uniqueSongs
if(uniqueSongs == numberUniqueSongs): #check if count of unique
songs is 3
simulationWithUniqueSongs += 1
probUniqueSongs = simulationWithUniqueSongs/simulations #basic probability formula
print("After "+str(simulations)+" simulations, the probability that after " + str(numberUniqueSongs) +" songs are played that your professor listens to three unique songs is %s ." %probUniqueSongs)
averageUniqueSongsListened =
ceil(totalUniqueSongsListened/(numberUniqueSongs*simulations)) #as
in each
#simulation numberUniqueSongs songs are listened
print("After "+str(simulations)+" simulations, the average number
of unique songs that your professor listens to is %s ."
%averageUniqueSongsListened)
#main function handling the simulation upto 10000 times
def main() :
simulateSongs()
#Invoking main() function
if __name__ == "__main__":
main()