Skip to content

Commit 1165cd5

Browse files
committed
test: fix getCombination function
1 parent 8ee5c38 commit 1165cd5

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

snapshot.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,30 @@ function createProjectWithFeatureFlags(flags) {
1313

1414
const featureFlags = ['typescript', 'jsx', 'router', 'vuex', 'with-tests']
1515

16-
// FIXME: not correct
1716
function getCombinations(arr) {
1817
const combinations = []
1918

20-
for (let i = 0; i < arr.length; i++) {
21-
for (let j = i; j < arr.length; j++) {
22-
const combination = arr.slice(i, j + 1)
23-
combinations.push(combination)
19+
// The following code & comments are generated by GitHub CoPilot.
20+
21+
// for an array of 5 elements, there are 2^5 - 1= 31 combinations
22+
// (excluding the empty combination)
23+
// equivalent to the following:
24+
// [0, 0, 0, 0, 1] ... [1, 1, 1, 1, 1]
25+
// We can represent the combinations as a binary number
26+
// where each digit represents a flag
27+
// and the number is the index of the flag
28+
// e.g.
29+
// [0, 0, 0, 0, 1] = 0b0001
30+
// [1, 1, 1, 1, 1] = 0b1111
31+
32+
for (let i = 1; i < 1 << arr.length; i++) {
33+
const combination = []
34+
for (let j = 0; j < arr.length; j++) {
35+
if (i & (1 << j)) {
36+
combination.push(arr[j])
37+
}
2438
}
39+
combinations.push(combination)
2540
}
2641

2742
return combinations

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy