DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Permutations Computer (2)

04.22.2011
| 2533 views |
  • submit to reddit
        
###### '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 }