Skip to content

Absolute Permutation, Algorithms #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
prabaprakash opened this issue Jul 27, 2020 · 3 comments
Closed

Absolute Permutation, Algorithms #1

prabaprakash opened this issue Jul 27, 2020 · 3 comments

Comments

@prabaprakash
Copy link
Owner

prabaprakash commented Jul 27, 2020

Please list possible algo for solving these problems

@prabaprakash
Copy link
Owner Author

prabaprakash commented Jul 28, 2020

tree

let permArr = [],
    usedChars = [];
function permute(input) {
    // console.log(input)
    var i, ch;
    for (i = 0; i < input.length; i++) {
        // fixed 1, then process [2,3]
        ch = input.splice(i, 1)[0];
        // usedchars [1]
        usedChars.push(ch);
        if (input.length == 0) {
            permArr.push(usedChars.slice());
        }
        // processing [2,3]
        permute(input);
        // 
        input.splice(i, 0, ch);
        usedChars.pop();
    }
    return permArr
};
process.stdout.write(JSON.stringify(permute([1, 2, 3])));

@prabaprakash
Copy link
Owner Author

prabaprakash commented Jul 28, 2020

Other methods recursive

function perm(xs) {
    let ret = [];

    for (let i = 0; i < xs.length; i++) {
        let currentChar = xs.splice(i, 1)[0];
        let rest = perm(xs);
        if (!rest.length) {
            ret.push([currentChar])
        } else {
            for (let j = 0; j < rest.length; j++) {
                ret.push([currentChar].concat(rest[j]))
            }
        }
        xs.splice(i, 0, currentChar);
    }

    return ret;
}

@prabaprakash
Copy link
Owner Author

Understanding question plays a major rule, these one is nothing to do with permutation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant