Nothing Comes From Nothing

2009-09-13

I think this is fun:

def power_set(s)
  return [[]] if s==[]
  s=s.to_a
  res=[]
  ((2**s.size)...(2**(s.size+1))).each do |i|
    t=[]
    check=i.to_s(2)[1..-1]
    check=check.split("").map{|a| a=="1"}
    (0...(check.length)).each{|j|t.push(s[j]) if check[j]}
    res.push(t)
  end
  res
end

irb(main):007:0> a=[]
=> []
irb(main):008:0> 4.times{a=power_set(a)};a
=> [[], [[[], [[]]]], [[[]]], [[[]], [[], [[]]]], [[[[]]]],
 [[[[]]], [[], [[]]]], [[[[]]], [[]]], [[[[]]], [[]], [[],
 [[]]]], [[]], [[], [[], [[]]]], [[], [[]]], [[], [[]], [[],
 [[]]]], [[], [[[]]]], [[], [[[]]], [[], [[]]]], [[], [[[]]],
 [[]]], [[], [[[]]], [[]], [[], [[]]]]]

That is one nice set. If you could have any of the elements in that set, which would you choose?

Comments

There's some javascript trying to load some comments, and if you're reading this, it's probably not working.