From: "Marcin Mielżyński" Date: 2005-12-24T09:22:50+09:00 Subject: Re: Translate from Python to Ruby Sam Kong wrote: > Hi! > > I am trying to get a powerset (all subsets) of a set. > I couldn't find a code in Ruby, but I found a code in Python. > I'm trying to convert it to Ruby but it's not done easily. > > def powset(seq): > if seq: > head, tail = seq[:1], seq[1:] > for smaller in powset(tail): > yield smaller > yield head + smaller > else: > yield [] > > 1. Can somebody translate the Python code into Ruby? > (I have difficulty in understanding the yield part) > def powset seq unless seq.empty? head,tail = seq[0,1] , seq[1..-1] powset(tail){|smaller| yield smaller yield head + smaller } else yield [] end end powset([1,2,5]){|e| p e} not sure if it works for other examples lopex