Skip to content

Instantly share code, notes, and snippets.

@rorysaur
Created May 23, 2015 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rorysaur/a268062cb01ff4eb14f2 to your computer and use it in GitHub Desktop.
Save rorysaur/a268062cb01ff4eb14f2 to your computer and use it in GitHub Desktop.
recursive subsets
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