-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
84 lines (79 loc) · 1.99 KB
/
helpers.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function getDigits (n, base = 10) {
const digits = []
while (n >= 1) {
const d = n % base
digits.push(d)
n = (n - d) / base
}
if (digits.length === 0) digits.push(0)
return digits
}
function getAssignments (items, groups = 2) {
const combinations = []
const nCombinations = Math.pow(groups, items)
for (let i = 0; i < nCombinations; i++) {
const combiString = i.toString(groups).padStart(items, '0')
const combination = combiString.split('')
if (groups <= 10) combinations.push(combination.map(v => +v))
else combinations.push(combination)
}
return combinations
}
function getPermutations (items, required = items.length) {
const combinations = []
function recurse (assigned, unassigned) {
if (assigned.length < required) {
for (let i = 0; i < unassigned.length; i++) {
const next = unassigned.shift()
recurse([...assigned, next], unassigned)
unassigned.push(next)
}
} else {
combinations.push(assigned)
}
}
recurse([], items)
return combinations
}
function getCombinations (items, required) {
const combinations = []
function recurse (assigned, i) {
if (assigned.length < required) {
while (i < items.length) {
recurse([...assigned, items[i]], i + 1)
i++
}
} else {
combinations.push(assigned)
}
}
recurse([], 0)
return combinations
}
function getSplitCombinations (total, items) {
const combinations = []
function recurse (division, i) {
if (division.length < items - 1) {
while (i <= total) {
recurse(division.concat(i), i)
i++
}
} else {
division = [0, ...division, total]
const combination = []
for (let i = 1; i < division.length; i++) {
combination.push(division[i] - division[i - 1])
}
combinations.push(combination)
}
}
recurse([], 0)
return combinations
}
module.exports = {
getDigits,
getAssignments,
getPermutations,
getCombinations,
getSplitCombinations
}