-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-2.js
executable file
·69 lines (65 loc) · 1.43 KB
/
ch-2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/bin/node
"use strict"
function combinations(arr, k) {
let c = [];
for (let i = 0; i < k; i++) {
c.push(i);
}
c.push(arr.length);
c.push(0);
let out = [];
while (true) {
let inner = [];
for (let i = k-1; i >= 0; i--) {
inner.push(arr[c[i]]);
}
out.push(inner);
let j = 0;
while (c[j] + 1 == c[j + 1]) {
c[j] = j;
j += 1;
}
if (j >= k) {
break;
}
c[j] += 1;
}
return out;
}
function splitsameaverage(a) {
let ss = a.reduce((x, y) => x + y, 0);
let ml = a.length;
let mx = Math.floor(ml / 2);
let ssa = false;
for (let n = 1; n <= mx; n++) {
for (let c of combinations(a, n)) {
let ca = c.reduce((x, y) => x + y, 0);
if (ca / n == (ss - ca) / (ml - n)) {
ssa = true;
break;
}
}
if (ssa) {
break;
}
}
return ssa;
}
if (splitsameaverage([1, 2, 3, 4, 5, 6, 7, 8])) {
process.stdout.write("Pass");
} else {
process.stdout.write("FAIL");
}
process.stdout.write(" ");
if (!splitsameaverage([1, 3])) {
process.stdout.write("Pass");
} else {
process.stdout.write("FAIL");
}
process.stdout.write(" ");
if (splitsameaverage([1, 2, 3])) {
process.stdout.write("Pass");
} else {
process.stdout.write("FAIL");
}
process.stdout.write("\n");