Last active
May 21, 2019 19:32
-
-
Save nitsanavni/04f41226bd7923f1714b4b921a4eab26 to your computer and use it in GitHub Desktop.
subgroups exercise using recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require("lodash"); | |
function subgroups(elements) { | |
if (elements.length == 1) { | |
return [elements]; | |
} | |
const drop = subgroups(_.drop(elements)); | |
const first = _.take(elements); | |
const combined = [..._.map(drop, (i) => [...i, ...first]), ...[...drop, first]]; | |
return combined; | |
} | |
describe("subgroups", () => { | |
_(_.range(2, 6)) | |
.map((n) => _.range(1, n)) | |
.each((inputElements) => { | |
describe(`${inputElements}`, () => { | |
let res; | |
beforeAll(() => (res = subgroups(inputElements))); | |
it("should ~2^n", () => expect(res.length).toBe((1 << inputElements.length) - 1)); | |
it("should unique", () => expect(_.uniq(res).length).toBe((1 << inputElements.length) - 1)); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool :)