Created
January 18, 2012 00:16
-
-
Save willf/1629957 to your computer and use it in GitHub Desktop.
Split into random slices from an array in Ruby
This file contains hidden or 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
class Array | |
def split_random(n) | |
def td(n) | |
[take(n),drop(n)] | |
end | |
def split_random_acc(n,l, acc) | |
head,tail = l.td(rand(n)+1) | |
return (acc + [head]) if tail==[] | |
split_random_acc(n, tail, acc + [head]) | |
end | |
split_random_acc(n,self,[]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment