Skip to content

Instantly share code, notes, and snippets.

@kharioki
Created October 11, 2016 18:48
Show Gist options
  • Save kharioki/fb1f3b596b4c16d1c34225001f89494c to your computer and use it in GitHub Desktop.
Save kharioki/fb1f3b596b4c16d1c34225001f89494c to your computer and use it in GitHub Desktop.
#sort playlist
def sort_music_asc(a):
i = 0
m = a[i][1]
for m in range(len(a)-1,0,-1):
for i in range(m):
if a[i][1]>a[i+1][1]:
temp = a[i]
a[i] = a[i+1]
a[i+1] = temp
return a
#add sorted playlist
#return playlist with max number of songs
def playlist(a, t):
a = sort_music_asc(a)
playtime = 0
play_list = []
i = 0
while i < len(a)-1:
playtime = playtime + a[i][1]
if playtime <= t:
play_list.append(a[i])
else:
break
i += 1
return play_list
a = [('d',5),('s',6),('w',2),('q',3),('w',3),('q',2),('l',4),('t',4),('y',7)]
t = 25
print(playlist(a, t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment