Last active
August 29, 2015 14:04
-
-
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
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
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