I’m using Ruby and needed to somehow generate all permutations of the digits in a given number and store them in an array. So for example I have number n = 9431, I need to generate all possible ways you can order those four numbers and store them in an array (The original can be in the array).
So if I input 9431 I need to get an array back with something like:
[9413, 9431, 9143, 9134, 9314, 9341, 4913, 4931, 4193, 4139, 4319, 4391, 1493, 1439, 1943, 1934, 1394, 1349, 3419, 3491, 3149, 3194, 3914, 3941]
Using strings is fine actually.
Clarification: Oh, and the output must be whole numbers in the array, not their individual digits.
This is “cheating”, because it uses intermediate strings which you didn’t want to do, but it works: