Skip to content

Instantly share code, notes, and snippets.

@ciniglio
Created December 10, 2012 01:22
Show Gist options
  • Save ciniglio/4247861 to your computer and use it in GitHub Desktop.
Save ciniglio/4247861 to your computer and use it in GitHub Desktop.
Greplin challenge p3
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