-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (46 loc) · 1.41 KB
/
Copy pathscript.js
File metadata and controls
54 lines (46 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Troy S. Wood
//06-12-2015
//
//Coderbyte Challenge - PermutationStep
//
//Using the JavaScript language, have the function PermutationStep(num)
//take the num parameter being passed and return the next number greater
//than num using the same digits. For example: if num is 123 return 132,
//if it's 12453 return 12534. If a number has no greater permutations,
//return -1 (ie. 999).
function permutationStep(num) {
var permArr = [],
usedChars = [];
function permute(numArr) {
var i, ch;
for (i = 0; i < numArr.length; i++) {
ch = numArr.splice(i, 1)[0];
usedChars.push(ch);
if (numArr.length === 0) {
permArr.push(usedChars.slice());
}
permute(numArr);
numArr.splice(i, 0, ch);
usedChars.pop();
}
return permArr;
}
permute(String(num).split(""));
for (var i = 0; i < permArr.length; i++) {
permArr[i] = Number(permArr[i].join(''));
}
permArr = permArr.sort(function (a, b) {
return a - b;
});
for (var j = 0; j < permArr.length; j++) {
if (permArr[j] > num) {
return permArr[j];
}
}
return -1;
}
console.log('Before: 999 | After: ' + permutationStep(999));
console.log('Before: 123 | After: ' + permutationStep(123));
console.log('Before: 12534 | After: ' + permutationStep(12534));
console.log('Before: 11121 | After: ' + permutationStep(11121));
console.log('Before: 41352 | After: ' + permutationStep(41352));