0

Hi guys I am working on the following CoderByte problem.

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).

Use the Parameter Testing feature in the box below to test your code with different arguments.

My code is

var PermutationStep = function(num){
var newNum = num.toString().split("");
for(var i = (newNum.length - 1); i >= 0; i--){
    if(newNum[i] > newNum[i-1]){
        newNum.splice(i-1,2,newNum[i],newNum[i-1]);
        var firstHalf = newNum.slice(0,i);
        var secondHalf = newNum.slice(i);
        break;
    }
    else{
        return -1;
    }

}

var sorted = secondHalf.sort(function(a,b){return a - b});
return firstHalf.concat(sorted).join("");

};

My code consists of looping through an array of the number and switching the terms newNum[i-1] and newNum[i] when newNum[i] > newNum[i - 1] and then taking all numbers to the right of these two terms and sorting them. Logically this works, but my code is not behaving the way I would like it to. For example PermutationStep(112211) returns -1 when it should be returning 121112. Can anyone help me debug this?

4
  • why should PermutationStep(112211) return 121122 instead of 112212? Commented Oct 25, 2014 at 23:24
  • It's the next greatest number using the digits in the input. Commented Oct 25, 2014 at 23:42
  • but isn't 121122 bigger than 112212, and aren't both bigger than 112211? (just trying to see if i can figure out exactly what you need so i can find a solution) Commented Oct 25, 2014 at 23:45
  • Sorry my mistake it should be return 121112 the next largest number using only the digits in the input. Does that make sense? Commented Oct 25, 2014 at 23:58

2 Answers 2

1

Make sure you are dealing with numbers and not strings when doing comparisons/evaluating numbers... A big problem I have run into with JS is numbers being evaluated as a string. Try wrapping your newNum[i] variable in the Number() function like this Number(newNum[i]).

Try the below code.

var PermutationStep = function (num) {
    var newNum = num.toString().split("");
    for (var i = (newNum.length - 1); i >= 0; i--) {
        if (Number(newNum[i]) > Number(newNum[i - 1])) {
            newNum.splice(i - 1, 2, newNum[i], newNum[i - 1]);
            var firstHalf = newNum.slice(0, i);
            var secondHalf = newNum.slice(i);
            break;
        }
        else {
            return -1;
        }

    }

    var sorted = secondHalf.sort(function (a, b) { return Number(a) - Number(b) });
    return firstHalf.concat(sorted).join("");

};
Sign up to request clarification or add additional context in comments.

Comments

0

function permutationStep(num) {
    const permutations = Array.from(
        new Set( // use Set to filter for unique values
            permute(num.toString().split(''))
                .map(number => number.join(''))
                .map(Number)
        )
    );

    permutations.sort((a, b) => a - b);

    const largerPermutations = permutations.filter(
        permutation => permutation > num
    );

    if (largerPermutations.length > 0) {
        return largerPermutations[0];
    }
    return -1;
}

// https://en.wikipedia.org/wiki/Heap's_algorithm
// Iterative
function permute(arr) {
    let count = Array(arr.length).fill(0);

    const results = [arr.slice()];

    let i = 0;
    while (i < arr.length) {
        if (count[i] < i) {
            if (i % 2 === 0) {
                const temp = arr[0];
                arr[0] = arr[i];
                arr[i] = temp;
            } else {
                const temp = arr[count[i]];
                arr[count[i]] = arr[i];
                arr[i] = temp;
            }
            results.push(arr.slice());
            count[i]++;
            i = 0;
        } else {
            count[i] = 0;
            i++;
        }
    }
    return results;
}

console.log(permutationStep(41352))

1 Comment

Your answer could be improved by adding explanation for how the code works to help future visitors :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.