Generate a powerset of a set without keeping a stack in Erlang or Ruby

前端 未结 3 932
感情败类
感情败类 2020-12-10 19:50

I would like to generate a powerset of a rather big set (about 30-50 elements) and I know that it takes 2^n to store the powerset.

Is it possible to gen

3条回答
  •  暖寄归人
    2020-12-10 20:02

    Edit: Added the enumerator (as @Jörg W Mittag) if no block is given.

    class Array
      def powerset
        return to_enum(:powerset) unless block_given?
        1.upto(self.size) do |n|
          self.combination(n).each{|i| yield i}
        end
      end
    end
    # demo
    ['a', 'b', 'c'].powerset{|item| p item} # items are generated one at a time
    ps = [1, 2, 3, 4].powerset # no block, so you'll get an enumerator 
    10.times.map{ ps.next } # 10.times without a block is also an enumerator
    

    Output

    ["a"]
    ["b"]
    ["c"]
    ["a", "b"]
    ["a", "c"]
    ["b", "c"]
    ["a", "b", "c"]
    [[1], [2], [3], [4], [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
    

提交回复
热议问题