Created
December 10, 2012 01:22
-
-
Save ciniglio/4247861 to your computer and use it in GitHub Desktop.
Greplin challenge p3
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 subset_sum(a) | |
ret = [] | |
while a.length > 1 do | |
n = a.pop | |
find_sum_from_left(n, a).each do |arr| | |
ret << (arr + [n]) | |
end | |
end | |
return ret | |
end | |
def find_sum_from_left(n, a) | |
ret = [] | |
0.upto(a.length - 1) do |i| | |
break if a[i] > n | |
ret << [a[i]] if a[i] == n | |
find_sum_from_left(n-a[i], a[0...i]).each do |arr| | |
ret << arr + [a[i]] | |
end | |
end | |
return ret | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment