Created
May 23, 2015 20:00
-
-
Save rorysaur/a268062cb01ff4eb14f2 to your computer and use it in GitHub Desktop.
recursive subsets
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
def recursive_subsets(array) | |
return [] if array.empty? | |
inner_array = array.first | |
if array.length == 1 | |
return inner_array.map { |item| [item] } | |
end | |
results = [] | |
rest_subsets = recursive_subsets(array[1..-1]) | |
inner_array.each do |item| | |
rest_subsets.each do |subset| | |
results << [item] + subset | |
end | |
end | |
results | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment