Jump to content

Can't find answer to my exercise: music shuffling

Featured Replies

Hi there, 

Edit: I spotted the mistake already, although i have to go now and have no time to change it completely!!! Please don't waste your time on it!


I am (still) working my way through a python book and a question came up but I am not 100% if the answer I produce is correct (and the book does not have an answer to this particular question).
The question is as follows: 

Music shuffling. You set your music player to shuffle mode. It plays each of the m songs before repeating any. Compose a program to estimate the likelihood that you will not hear any sequential pair of songs (that is, song 3 does not follow song 2, song 10 does not follow song 9, and so on).
From: Sedgewick, Robert. Introduction to Programming in Python (p. 135). Pearson Education. Kindle Edition. 

I have the following code:
 

# # 1.4.24

m = 1000 #number of songs
n = 1000 #attempts
counter = 0

Songs1 = stdarray.create1D(m,0) #create empty array with songs
for i in range(len(Songs1)): # number each song 
    Songs1 = i+1 

for g in range(n):
    Songs = Songs1[:] #copy Songs1 so that we don't have to do the initialising within this part of the loop again 
    for i in range(m-1): #randomly shuffle the songs
        r = random.randrange(i, m) 
        temp = Songs[r] 
        Songs[r] = Songs 
        Songs = temp
    for i in range(m-1): 
        if Songs[i+1]-(Songs[i]) == 1: #check if, after shuffling, the songs are in sequential order, if so add 1 to counter
            counter +=1
            break
        # else: 
        #     print(Songs) # check if none-counted songs actually do not contain any things in sequential order
print(100-counter/n*100) # calculate percentage

I think, based on the checks I did, that this code should work. However I find the numbers (34-39%) quite high for the current numbers (n = 1000, m =1000), and therefore feel that I may have made a mistake.

Thanks in advance

Edit: I spotted the mistake already, although i have to go now and have no time to change it completely!!!
the commented out else statement also shows [3, 1, 2] that is not counterd within the last if statement, i will figure it out later and come with an answer so that other people may learn from it!

Edited by Strange
Fix code formatting

  • Author

Alright, so I feel a bit (not a lot luckily) stupid, in the heat of the moment I thought my code was broken, but of course the else statement will show [3,1,2] once, before it finds the 1,2 sequence. I also see that for some reason (I suppose it is the command for Italic) the [ i ] in my Songs [ i ] has disappeared. 

I have manually checked my code a few more times and suppose it is working as intended, so my original question is now, once again, applicable I think: does this code really produce the right results. It feels very counter intuitive to me (but of course, it can just be that; a wrong feeling), that in a randomly shuffled list of 1000 songs, it was managed to be shuffled around 35-40% of the time in such a way that there are no sequential numbers... 

  • Author
9 hours ago, Strange said:

I tried to fix the formatting for you (manly so I could read it!)

Thanks Strange, you're an angel!! By the time I got back I couldn't edit my own post anymore 

Edited by Dagl1

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.