Input is a single integer in ascending digit order.
The only valid inputs are:
12
123
1234
12345
123456
1234567
12345678
123456789
The only valid output is an array (set; list) of length
equal to the factorial of input:
Input - Factorial of input - Output array length
12
-> 1*2
-> 2
123
-> 1*2*3
-> 6
1234
-> 1*2*3*4
-> 24
12345
-> 1*2*3*4*5
-> 120
123456
-> 1*2*3*4*5*6
-> 720
1234567
-> 1*2*3*4*5*6*7
-> 5040
12345678
-> 1*2*3*4*5*6*7*8
-> 40320
123456789
-> 1*2*3*4*5*6*7*8*9
-> 362880
The output array is all integers of input length in lexicographical order.
For example, given input 123
-> factorial of input is 1*2*3=6
-> for each of the six array elements output the input integer and the five remaining integers of the array in lexicographic order -> [123,132,213,231,312,321]
. Notice that the last element is always the input integer in reverse order.
Test Cases
Input Output `12` -> `[12,21]` // valid output, ascending numeric order `12` -> `[12.0,21.0]` // invalid output, output must be rounded to integer `123` -> `[123,132,213,231,312,321]` // valid output `123` -> `[123,132,213,222,231,312,321]` // invalid output: `222` are duplicate digits `123` -> `[123,142,213,231,213,321]` // invalid output: `142` outside range `1-3` `123456789` -> `[987654321,...,123456789]` // valid output, descending numeric order `123456789` -> `[987654321,...,123456798]` // invalid output, `123456798` is greater than the minimum required integer in resulting array `123456789`
Rules
- Do not use standard library functions for permutations or combinatorics.
- If the algorithm produces an integer greater or less than input minimum or maximum only the integers in the specified range must be output. (Technically, if input is
12345
we could generate all integers to21
or12
, though for input12345
the only valid output must be within the range12345
through54321
). - Output must be an array (set; list) of integers, not strings.
- The integers output must not be hardcoded.
- The output integers must not include duplicate digits (more than one of the same digit at that array index).
Winning criteria
Hardware, OS
Where submissions will be run
~$ lscpu Architecture: i686 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 1 Core(s) per socket: 2 Socket(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 37 Model name: Intel(R) Pentium(R) CPU P6200 @ 2.13GHz Stepping: 5 CPU MHz: 2133.000 CPU max MHz: 2133.0000 CPU min MHz: 933.0000 BogoMIPS: 4256.26 L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 3072K ~$ free -m total used free shared buff/cache available Mem: 3769 1193 802 961 1772 1219 Swap: 0 0 0 ~$ uname -r 4.8.0-36-lowlatency
Kindly explain the algorithm within the body of the answer.
For languages used other than JavaScript (which will test at Chromium 70) will take the time to install the language and test the code for the time taken to output the requirement, as there is no way (that have found) to provide uniform results as to time for every possible language used to derive output (see this comment).