Skip to content

Instantly share code, notes, and snippets.

@eqdw
Created October 9, 2010 19:26
Show Gist options
  • Save eqdw/618518 to your computer and use it in GitHub Desktop.
Save eqdw/618518 to your computer and use it in GitHub Desktop.
#this code borrowed from the internet
class Array
# Returns the "power set" for this Array. This means that an array with all
# subsets of the array's elements will be returned.
def power
# the power set line is stolen from http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
inject([[]]) do |c,y|
r=[]
c.each do |i|
r<<i
r<<i+[y]
end
r
end
end
end
#end borrowed section
@nums = [3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99]
@count = 0
puts "BEFORE POWERSET"
all_sets = @nums.power
puts "AFTER POWERSET"
i = 0
puts "sets to process: #{all_sets.size}"
all_sets.each do |subset|
if i % 20 == 0
puts "DEBUG: processing subset #{i} of #{all_sets.size}: #{i.to_f / all_sets.size}% complete"
end
subset.sort!
max = subset.pop
@count += 1 if max == subset.inject(0){|sum,item| sum+item}
i += 1
end
puts "count: #{@count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment