Last active
February 17, 2018 03:20
-
-
Save yhirano55/8aa6d71f7343b1806763997ecbeef791 to your computer and use it in GitHub Desktop.
Calculate robots sample
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 calculate(n) | |
[1, 2, 3].repeated_combination(n).to_a.each_with_object({}) { |a, cache| | |
Array.new(n) do |i| | |
combination = a[0..i] | |
if combination.sum == n && !cache.key?(combination) | |
cache[combination] = true | |
end | |
end | |
}.keys | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment