Over a million developers have joined DZone.

Permutations Computer (2)

·

###### '0123' ==> '1234'
def conv(base,s)
 s.split('').map {|c| (c.to_i(base)+1).to_s(base+1)}
end

def permutation(n)
  num=(n**n) - 1
  (0..num).to_a.map do|u| 
     sn  = u.to_s(n)        # num in base n
     str =sn.rjust(n,'0')   # left pad with '0'
     conv(n,str) 
  end
end


permutation(ARGV[0].to_i).each { |i| p i }

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}