-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutilities.js
More file actions
54 lines (47 loc) · 1.24 KB
/
utilities.js
File metadata and controls
54 lines (47 loc) · 1.24 KB
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
const debruijn = (k, n) => {
const a = [...Array(k * n)].fill(0);
let sequence = [];
const db = (t, p) => {
if (t > n) {
if (n % p === 0) {
sequence = sequence.concat(a.slice(1, p + 1));
}
} else {
a[t] = a[t - p];
db(t + 1, p);
for (let j = a[t - p] + 1; j <= k; j++) {
a[t] = j;
db(t + 1, t);
}
}
};
db(1, 1);
return sequence.join('');
};
const binaryStreamStringToBytes = (string) => {
// 1100110001011001 -> 11001100 01011001
let chunks = string.split(/(.{8})/).filter((x) => x);
// 11001100 01011001 -> 00110011 10011010
// chunks = chunks.map((x) => [...x].reverse().join(''));
// 00110011 10011010 -> 0x33 0x9a
chunks = chunks.map((x) =>
`0x${parseInt(x, 2).toString(16).padStart(2, 0)}`);
return chunks.join(', ');
};
const printChunks = (byteString) =>
byteString.split(' ').reduce((s, x, i) => {
const p = Math.floor(i / 9);
s[p] = (s[p] || []).concat(x);
return s;
}, []).forEach((x) => console.log(x.join(' ')));
module.exports = {
binaryStreamStringToBytes,
debruijn,
printChunks,
};
if (require.main === module) {
// de bruijn for hekapoo.h
console.log(printChunks(
binaryStreamStringToBytes(
debruijn(1, 10))));
}