From: Michael 'entropie' Trommer Date: 2005-12-28T02:42:17+09:00 Subject: Re: Translate from Python to Ruby --/TUrtqMIkCP4YtJm Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable * Simon Kr?ger (SimonKroeger@gmx.de) wrote: > Sorry, i couldn't resist: >=20 > class Array > def powset > (0...(2**size)).map do |n| > (0...size).reject{|i|n[i].zero?}.map{|i|at(i)} > end > end > end lovely peace of code! (it makes my brain burning but its nice!) >=20 > cheers >=20 > Simon >=20 > Stefan Walk wrote: >=20 > >Non-recursive version: > >I'm sure you can translate this for sets... > > > >class Array > > def powset > > ret =3D [] > > 0.upto(2**size - 1) do |n| > > ret << [] > > 0.upto(size - 1) do |i| > > ret.last << self[i] if (n & (1 << i)) !=3D 0 > > end > > end > > ret > > end > >end > > > >p [].powset > >p [1].powset > >p [1,2].powset > >p [1,2,3].powset > >[[]] > >[[], [1]] > >[[], [1], [2], [1, 2]] > >[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]] > > > > >=20 >=20 So long -- Michael 'entropie' Trommer; http://ackro.org ruby -e "0.upto((a=3D'njduspAhnbjm/dpn').size-1){|x| a[x]-=3D1}; p 'mailto:= '+a" --/TUrtqMIkCP4YtJm Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) iD8DBQFDsXzoBBd8ye5RguQRAp6oAKCwxfUXPW+PZFibuDGBnDolvcFdWgCfSfFi o1iwYxkXrbSKsLBdABQ4NOU= =daJd -----END PGP SIGNATURE----- --/TUrtqMIkCP4YtJm--