-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathcode.js
More file actions
78 lines (73 loc) · 1.85 KB
/
Copy pathcode.js
File metadata and controls
78 lines (73 loc) · 1.85 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// import visualization libraries {
const { Tracer, Array2DTracer, LogTracer, Randomize, Layout, VerticalLayout } = require('algorithm-visualizer');
// }
// define tracer variables {
const tracer = new Array2DTracer();
const logger = new LogTracer();
Layout.setRoot(new VerticalLayout([tracer, logger]));
const integer = Randomize.Integer({ min: 5, max: 14 });
const D = [];
const A = "";
for (let i = 0; i <= integer; i++) {
D.push([]);
D[i][0] = 1
for (let j = 1; j <= integer; j++) D[i][j] = 0;
}
tracer.set(D);
Tracer.delay();
// }
function partition(A, n, p) {
// logger {
if (p == 0) logger.println(`[${A.split('').join(', ')}]`);
// }
else {
if (n > 1) partition(A, n - 1, p);
if (n <= p) partition(n + A, n, p - n);
}
}
function integerPartition(n) {
// cycle through each cell of matrix
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
if (i > j) {
// visualize {
tracer.select(i, j);
Tracer.delay();
// }
// set cell to cell above it
D[i][j] = D[i - 1][j];
// visualize {
tracer.patch(i, j, D[i][j]);
Tracer.delay();
tracer.depatch(i, j);
tracer.deselect(i, j);
// }
}
else {
// visualize {
tracer.select(i, j);
Tracer.delay();
// }
// grab above cell and add it to previous cell
const above = D[i - 1][j];
const left = D[i][j - i];
D[i][j] = above + left;
// visualize {
tracer.patch(i, j, D[i][j]);
Tracer.delay();
tracer.depatch(i, j);
tracer.deselect(i, j);
// }
}
}
}
return D[n][n];
}
// logger {
logger.println(`Partitioning: ${integer}`);
// }
partition(A, integer, integer);
const part = integerPartition(integer);
// logger {
logger.println(part);
// }