Skip to content

Instantly share code, notes, and snippets.

@chischaschos
Last active August 29, 2015 14:04
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 chischaschos/df49cb31f287ec6b8943 to your computer and use it in GitHub Desktop.
Save chischaschos/df49cb31f287ec6b8943 to your computer and use it in GitHub Desktop.
Given an array of N numbers select all the possible groups of size 1..N that may be selected
def word_combinations(words, original_set_size = words.count, start_index = 0, end_index = words.count - 1, combinations = 1)
words[start_index..end_index].each do |combination|
words[0..original_set_size - 1].each do |original_combination|
unless combination.include?(original_combination.first)
new_combination = (combination + original_combination).sort
unless words.include?(new_combination)
words << new_combination
end
end
end
end
combinations += 1
return words if combinations == original_set_size
word_combinations(words, original_set_size, end_index + 1, words.count - 1, combinations)
end
pp word_combinations [['uno'], ['dos'], ['tres']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment