array permutations

A

Alex Ciarlillo

The other day I ran into a problem where I needed all the permutations
of a given array. I knew I had covered this in some of my CS classes but
couldn't come up with the algorithm at first. I figured it out on the
drive home from work and decided to rubify it and add it as a method to
the array class. This is what I came up with and was just wondering if
anyone else had a cleaner or more effecient way of accomplishing this.

class Array
def each_perm
if self.size == 1
yield self
else
self.each_index do |i|
tmp, e = self.dup, self
tmp.delete_at(i)
tmp.each_perm do |x|
yield e.to_a + x
end
end
end
end
end


--AC
 
D

Dan Zwell

Alex said:
The other day I ran into a problem where I needed all the permutations
of a given array. I knew I had covered this in some of my CS classes but
couldn't come up with the algorithm at first. I figured it out on the
drive home from work and decided to rubify it and add it as a method to
the array class. This is what I came up with and was just wondering if
anyone else had a cleaner or more effecient way of accomplishing this.

class Array
def each_perm
if self.size == 1
yield self
else
self.each_index do |i|
tmp, e = self.dup, self
tmp.delete_at(i)
tmp.each_perm do |x|
yield e.to_a + x
end
end
end
end
end


--AC


Have a look at this thread:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/247669 .
It's a port of the GCC library version to ruby. As for efficiency,
you'll have to run them both (I haven't read either version carefully).
One factor that might make his slower is that it should avoids yielding
duplicates.

Have fun,
Dan
 
R

Ryan Davis

The other day I ran into a problem where I needed all the permutations
of a given array. I knew I had covered this in some of my CS
classes but
couldn't come up with the algorithm at first. I figured it out on the
drive home from work and decided to rubify it and add it as a
method to
the array class. This is what I came up with and was just wondering if
anyone else had a cleaner or more effecient way of accomplishing this.

The one thing I learned in Algs is someone has invented/implemented/
tested it before me:

http://rubygarden.org/Ruby/page/show/ArrayPermute

Please use google.
 
K

Ken Bloom

Alex said:
The other day I ran into a problem where I needed all the permutations
of a given array. I knew I had covered this in some of my CS classes
but couldn't come up with the algorithm at first. I figured it out on
the drive home from work and decided to rubify it and add it as a
method to the array class. This is what I came up with and was just
wondering if anyone else had a cleaner or more effecient way of
accomplishing this.

class Array
def each_perm
if self.size == 1
yield self
else
self.each_index do |i|
tmp, e = self.dup, self
tmp.delete_at(i)
tmp.each_perm do |x|
yield e.to_a + x
end
end
end
end
end


--AC


Have a look at this thread:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/247669 .
It's a port of the GCC library version to ruby. As for efficiency,
you'll have to run them both (I haven't read either version carefully).
One factor that might make his slower is that it should avoids yielding
duplicates.


There is a version of each_permutation in the Facets library (http://
facets.rubyforge.org/) which is similar to the version you posted. After
relying on their version for several Ruby Quizzes, I decided that it
wasn't as useful to me as I would have liked, because I found I had to do
bookkeeping for duplicates anyway, so I ported over the C++ STL version
which doesn't generate duplicates (because it's stateless).

Different design goals.

--Ken
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top