Translate from Python to Ruby

S

Sam Kong

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)

2. Actually I want to add a method to Set class.
How can I change the function into a method which won't take any
parameter.
(Can a method be recursive if it doesn't take a parameter?
Maybe not.
Do I need a helper private method?)
example:
s = Set[1,2,3]
ps = s.powerset #returns a set of powerset of s

Thanks.

Sam
 
M

Marcin Mielżyński

Sam said:
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
 
S

Stefan Walk

Non-recursive version:
I'm sure you can translate this for sets...

class Array
def powset
ret = []
0.upto(2**size - 1) do |n|
ret << []
0.upto(size - 1) do |i|
ret.last << self if (n & (1 << i)) != 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]]
 
S

Simon Kröger

Sorry, i couldn't resist:

class Array
def powset
(0...(2**size)).map do |n|
(0...size).reject{|i|n.zero?}.map{|i|at(i)}
end
end
end

cheers

Simon
 
K

Kenneth Collins

require 'set'

class Set
def powerset
result = Set.new
for element_map in 0...(1 << self.length) do
subset = Set.new
each_with_index do |element, index|
subset.add(element) if element_map[index] == 1
end
result.add(subset)
end
return result
end
end

s = Set[1,2,3]
ps = s.powerset #returns a set of powerset of s
p ps
 
M

Michael 'entropie' Trommer

--/TUrtqMIkCP4YtJm
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* Simon Kr?ger ([email protected]) said:
Sorry, i couldn't resist:
=20
class Array
def powset
(0...(2**size)).map do |n|
(0...size).reject{|i|n.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 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--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top