Created
March 29, 2016 14:15
-
-
Save cmdcolin/0b1ea83de19f27d6fee5 to your computer and use it in GitHub Desktop.
Given a list of song durations, split them into two parts of equal length using knapsack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# given a list of song durations, split them into two parts of equal length using knapsack | |
songs=list("drugs"=6*60+40,"error"=5*60+36,"stepvhen"=5*60+17,"infinity"=6*60+13, | |
"pete"=2*60+38,"Zalh"=7*60+3,"morpheus"=5*60+4,"cleartone"=6*60+5, | |
"dedmeth"=6*60+32,"orthodox"=2*60+18,"xeph"=4*60+12,"amelia where are"=6*60+58) | |
half.cassette <- local ( | |
function (target) { | |
c <- gtools::combinations(length(songs), r=6, v=unlist(songs)) | |
s=rowSums(c) | |
x <- which( abs(s-target) < 2 ) | |
c[x,] | |
}) | |
half.cassette(sum(unlist(songs))/2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment