Hi, I would like the following python code rewritten in a different way/structure etc. I got...

70.2K

Verified Solution

Question

Programming

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__\":

Answer & Explanation Solved by verified expert
3.7 Ratings (626 Votes)
Changes done in structure made static 3 songs to dynamic number so that simulation can be found of any number For unique purpose threeUniqueSongspy from future import printfunction 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 getRandomSongsongPlaylist randomly choose a song from songPlaylist with    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students