Last active
January 29, 2016 07:15
-
-
Save nickrobson/3366bfdcc660892fb395 to your computer and use it in GitHub Desktop.
Gets all permutations of a given number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def number_shuffle(number) | |
dig = number.to_s.split '' | |
if dig.length == 1 | |
return dig | |
end | |
out = [] | |
(0...dig.length).each do |d| | |
nums = [] | |
if d > 0 | |
nums = nums << dig[0..d-1] | |
end | |
if d < dig.length-1 | |
nums = nums << dig[d+1..dig.length-1] | |
end | |
number_shuffle( nums.join('').to_i ).each do |i| | |
out.push (i.to_s + dig[d].to_s).to_i | |
end | |
end | |
out.sort | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment