Question

In: Computer Science

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

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

Solutions

Expert Solution

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()


Related Solutions

I would like the following code in PYTHON please, there was a previous submission of this...
I would like the following code in PYTHON please, there was a previous submission of this question but I am trying to have the file save as a text file and not a json. Any help would be greatly appreciated. Create an application that does the following: The user will enter product codes and product numbers from the keyboard as strings. The product code must consist of four capital letters. The product number can only consist of numeric characters, but...
Hi, I need the HTML5 code for the below. About Me **Would like to add a...
Hi, I need the HTML5 code for the below. About Me **Would like to add a image of a plane or something related to travel here. Mia Jo I am taking this class to earn my Computer programmer CL1. Things I Like to Do: Spend time with family Traveling People Watch Places I Want to Go or Have Visited: Dubai- December'18 Enjoyed shopping and the desert safari the most. Cuba- August '18 Enjoyed learning about the culture and history. China-...
hi i do not know what is wrong with my python code. this is the class:...
hi i do not know what is wrong with my python code. this is the class: class Cuboid: def __init__(self, width, length, height, colour): self.__width = width self.__length = length self.__height = height self.__colour = colour self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height)) self.volume = height * length * width def get_width(self): return self.__width def get_length(self): return self.__length def get_height(self): return self.__height def get_colour(self): return self.__colour def set_width(self,...
Hi, I would be grateful for some helot with this Python problem. Your task is to...
Hi, I would be grateful for some helot with this Python problem. Your task is to implement the simple elevator in Python using classes. The default strategy is the simple "start at the bottom, go to the top, then go to the bottom". Can you write a better strategy, one that is more efficient? Description / Specification Create three classes: Building, Elevator, and Customer. Equip the building with an elevator. Ask user to customize the number of floors and the...
Hi, I would like to know the answer in the following book. Investments. 11 Edition, from...
Hi, I would like to know the answer in the following book. Investments. 11 Edition, from chapter 1 - Problem Sets: 6 question: Q6) Suppose housing prices across the world double. a. Is society any richer for the change? b. Are homeowners wealthier? C. Can you reconcile your answers to (a) and (b)? Is anyone worse off as a result of the change? I don't have any details other than my textbook. Please let me know what else do you...
Hi, i would like to request assistance with the following question: Bert computes a 95% confidence...
Hi, i would like to request assistance with the following question: Bert computes a 95% confidence interval for p and obtains the interval [0.600, 0.700]. Note: Parts (a) and (b) are not connected: Part (b) can be answered even if one does not know how to do part (a). (a) Bert’s boss says, “Give me a 90% confidence interval for p.” Calculate the answer for Bert. (b) Bert’s boss says, “Give me a 95% confidence interval for p−q.” Calculate the...
Hi, I would like the numerical solution for this task so I can be able to...
Hi, I would like the numerical solution for this task so I can be able to model it in a kinetic modeling programme like Berkely Madonna (BM). This was the reason why I started my membership with Chegg Study but unfortunately, the solution to this problem was not available. I'm looking forward to hearing from you. you will find the task in de link below. With kind regards, Ahmed https://www.chegg.com/homework-help/comprehensive-problem-multiple-reactions-heat-effects-styren-chapter-12-problem-24qp-solution-9780132317160-exc in J.Snyder en B.Subramaniam, Chem. Eng. Sci., 49, 5585 (1994)....
Hi I would like to see an example in c++ of Stack As Linked List I...
Hi I would like to see an example in c++ of Stack As Linked List I need a seek() function which receives a double number X and which returns in which position in the stack is X. If the value X is not in the stack, the function should return -1
what is the solution of this code? Also how would I run it in python to...
what is the solution of this code? Also how would I run it in python to check? q = Queue() q.enqueue(10) q.enqueue(12) q.enqueue(15) for i in range(len(q)): → if len(q) % 2 == 0: → → q.enqueue(q.dequeue()) → else: → → q.dequeue() print(q)
Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT