Skip to content
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

solving unrelated issue - bounty #78

Open
ORESoftware opened this issue Oct 29, 2022 · 3 comments
Open

solving unrelated issue - bounty #78

ORESoftware opened this issue Oct 29, 2022 · 3 comments

Comments

@ORESoftware
Copy link
Owner

This problem is unrelated to this repo:
https://gist.github.com/ORESoftware/941eabac77cd268c826d9e17ae4886fa

Trying to get the above routine created.

@cngzhnp
Copy link

cngzhnp commented Oct 31, 2022

function combine(k, arr, prefix=[]) {
    if(arr.length > k) {
        throw new Error('Not enough elements to combine.');
    }
    if (k == 0) return [prefix];
    return arr.flatMap((v, i) =>
        combine(k-1, arr.slice(i+1), [...prefix, v])
    );
}

@ORESoftware
Copy link
Owner Author

interesting technique

@mystixxx
Copy link

mystixxx commented Dec 26, 2022

function combine(x: number, list: number[]): number[][] {
    if (x === 0) {
        return [[]];
    } else if (x === 1) {
        return list.map(e => [e]);
    } else {
        const combinations: number[][] = [];
        for (let i = 0; i < list.length; i++) {
            for (const subcombination of combine(x - 1, list.slice(i + 1))) {
                combinations.push([list[i], ...subcombination]);
            }
        }
        return combinations;
    }
}

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

No branches or pull requests

3 participants