Skip to content

Commit f5f68df

Browse files
committed
Rename method wait to delay, notify to patch, and denotify to depatch
1 parent b87f7d5 commit f5f68df

File tree

77 files changed

+547
-547
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+547
-547
lines changed

Backtracking/Knight's tour problem/code.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pos[0] = pos[1] = -1;
2828

2929
const boardTracer = new Array2DTracer('Board').set(board);
3030
const posTracer = new Array1DTracer('Knight Position').set(pos);
31-
const logTracer = new LogTracer('Console').wait();
31+
const logTracer = new LogTracer('Console').delay();
3232

3333
function knightTour(x, y, moveNum) {
3434
if (moveNum === N * N) {
@@ -39,10 +39,10 @@ function knightTour(x, y, moveNum) {
3939
const nextX = x + X[i];
4040
const nextY = y + Y[i];
4141

42-
posTracer.notify(0, nextX).wait();
43-
posTracer.notify(1, nextY).wait();
44-
posTracer.denotify(0);
45-
posTracer.denotify(1);
42+
posTracer.patch(0, nextX).delay();
43+
posTracer.patch(1, nextY).delay();
44+
posTracer.depatch(0);
45+
posTracer.depatch(1);
4646
/*
4747
Check if knight is still in the board
4848
Check that knight does not visit an already visited square
@@ -51,8 +51,8 @@ function knightTour(x, y, moveNum) {
5151
board[nextX][nextY] = moveNum;
5252

5353
logTracer.print(`Move to ${nextX},${nextY}`);
54-
boardTracer.notify(nextX, nextY, moveNum).wait();
55-
boardTracer.denotify(nextX, nextY);
54+
boardTracer.patch(nextX, nextY, moveNum).delay();
55+
boardTracer.depatch(nextX, nextY);
5656
boardTracer.select(nextX, nextY);
5757

5858
const nextMoveNum = moveNum + 1;
@@ -61,8 +61,8 @@ function knightTour(x, y, moveNum) {
6161
}
6262
logTracer.print(`No place to move from ${nextX},${nextY}: Backtrack`);
6363
board[nextX][nextY] = -1; // backtrack
64-
boardTracer.notify(nextX, nextY, -1).wait();
65-
boardTracer.denotify(nextX, nextY);
64+
boardTracer.patch(nextX, nextY, -1).delay();
65+
boardTracer.depatch(nextX, nextY);
6666
boardTracer.deselect(nextX, nextY);
6767
} else {
6868
logTracer.print(`${nextX},${nextY} is not a valid move`);
@@ -75,13 +75,13 @@ board[0][0] = 0; // start from this position
7575
pos[0] = 0;
7676
pos[0] = 0;
7777

78-
boardTracer.notify(0, 0, 0).wait();
79-
posTracer.notify(0, 0).wait();
80-
posTracer.notify(1, 0).wait();
81-
boardTracer.denotify(0, 0);
82-
boardTracer.denotify(0, 0);
83-
posTracer.denotify(0);
84-
posTracer.denotify(1);
78+
boardTracer.patch(0, 0, 0).delay();
79+
posTracer.patch(0, 0).delay();
80+
posTracer.patch(1, 0).delay();
81+
boardTracer.depatch(0, 0);
82+
boardTracer.depatch(0, 0);
83+
posTracer.depatch(0);
84+
posTracer.depatch(1);
8585

8686
if (knightTour(0, 0, 1) === false) {
8787
logTracer.print('Solution does not exist');

Backtracking/N Queens Problem/code.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const logger = new LogTracer('Progress');
2222

2323
boardTracer.set(board);
2424
queenTracer.set(queens);
25-
logger.print(`N Queens: ${N}X${N}matrix, ${N} queens`).wait();
25+
logger.print(`N Queens: ${N}X${N}matrix, ${N} queens`).delay();
2626

2727
function validState(row, col, currentQueen) {
2828
for (let q = 0; q < currentQueen; q++) {
@@ -45,23 +45,23 @@ function nQ(currentQueen, currentCol) {
4545
let found = false;
4646
let row = 0;
4747
while ((row < N) && (!found)) {
48-
boardTracer.select(row, currentCol).wait();
48+
boardTracer.select(row, currentCol).delay();
4949
logger.print(`Trying queen ${currentQueen} at row ${row} & col ${currentCol}`);
5050

5151
if (validState(row, currentCol, currentQueen)) {
5252
queens[currentQueen][0] = row;
5353
queens[currentQueen][1] = currentCol;
5454

55-
queenTracer.notify(currentQueen, 0, row).wait();
56-
queenTracer.notify(currentQueen, 1, currentCol).wait();
57-
queenTracer.denotify(currentQueen, 0).wait();
58-
queenTracer.denotify(currentQueen, 1).wait();
55+
queenTracer.patch(currentQueen, 0, row).delay();
56+
queenTracer.patch(currentQueen, 1, currentCol).delay();
57+
queenTracer.depatch(currentQueen, 0).delay();
58+
queenTracer.depatch(currentQueen, 1).delay();
5959

6060
found = nQ(currentQueen + 1, currentCol + 1);
6161
}
6262

6363
if (!found) {
64-
boardTracer.deselect(row, currentCol).wait();
64+
boardTracer.deselect(row, currentCol).delay();
6565
logger.print(`row ${row} & col ${currentCol} didn't work out. Going down`);
6666
}
6767
row++;

Cryptography/Affine Cipher/code.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const ptTracer = new Array1DTracer('Encryption');
55
const ctTracer = new Array1DTracer('Decryption');
66
const logger = new LogTracer();
77

8-
ptTracer.set(plainText).wait();
8+
ptTracer.set(plainText).delay();
99

1010
/*
1111
code assumes that plainText contains ONLY LOWER CASE ALPHABETS
@@ -36,13 +36,13 @@ function encrypt(plainText) {
3636
logger.print(`keys.a=${keys.a}, keys.b=${keys.b}, N=${N}`);
3737

3838
for (const i in plainText) {
39-
ptTracer.select(i).wait();
39+
ptTracer.select(i).delay();
4040
ptTracer.deselect(i);
4141

4242
cypherText += cryptAlpha(plainText[i]);
4343

44-
ptTracer.notify(i, cypherText.slice(-1)).wait();
45-
ptTracer.denotify(i);
44+
ptTracer.patch(i, cypherText.slice(-1)).delay();
45+
ptTracer.depatch(i);
4646
}
4747

4848
return cypherText;
@@ -75,13 +75,13 @@ function decrypt(cypherText) {
7575
logger.print(`keys.b=${keys.b}, N=${N}`);
7676

7777
for (const i in cypherText) {
78-
ctTracer.select(i).wait();
79-
ctTracer.deselect(i).wait();
78+
ctTracer.select(i).delay();
79+
ctTracer.deselect(i).delay();
8080

8181
plainText += decryptAlpha(cypherText[i]);
8282

83-
ctTracer.notify(i, plainText.slice(-1)).wait();
84-
ctTracer.denotify(i).wait();
83+
ctTracer.patch(i, plainText.slice(-1)).delay();
84+
ctTracer.depatch(i).delay();
8585
}
8686

8787
return plainText;

Cryptography/Caesar Cipher/code.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const encryptTracer = new Array1DTracer('Encryption');
1515
const decryptTracer = new Array1DTracer('Decryption');
1616
const logger = new LogTracer();
1717

18-
encryptTracer.set(string).wait();
18+
encryptTracer.set(string).delay();
1919

2020
function getPosUp(pos) {
2121
return (pos === alphabet.length - 1) ? 0 : pos + 1;
@@ -38,19 +38,19 @@ function cipher(str, rotation, direction, cipherTracer) {
3838
if (!str) return '';
3939

4040
for (let i = 0; i < str.length; i++) {
41-
cipherTracer.wait();
41+
cipherTracer.delay();
4242

4343
let currChar = str.charAt(i);
4444
if (typeof alphabetMap[currChar] === 'number') { // don't encrpt/decrypt characters not in alphabetMap
4545
let r = rotation;
4646

4747
logger.print(`Rotating ${currChar} ${direction} ${rotation} times`);
48-
cipherTracer.select(i).wait();
48+
cipherTracer.select(i).delay();
4949

5050
// perform given amount of rotations in the given direction
5151
while (r-- > 0) {
5252
currChar = getNextChar(currChar, direction);
53-
cipherTracer.notify(i, currChar).wait();
53+
cipherTracer.patch(i, currChar).delay();
5454
}
5555
} else {
5656
logger.print('Ignore this character');

Dynamic Programming/Catalan Number/code.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ for (let i = N; i >= 0; i--) {
77
}
88

99
const tracer = new Array1DTracer(' Catalan Numbers ').set(A);
10-
const logger = new LogTracer().wait();
10+
const logger = new LogTracer().delay();
1111

1212
A[0] = 1;
13-
tracer.notify(0, A[0]).wait();
14-
tracer.denotify(0);
13+
tracer.patch(0, A[0]).delay();
14+
tracer.depatch(0);
1515
A[1] = 1;
16-
tracer.notify(1, A[1]).wait();
17-
tracer.denotify(1);
16+
tracer.patch(1, A[1]).delay();
17+
tracer.depatch(1);
1818

1919
for (let i = 2; i <= N; i++) {
2020
for (let j = 0; j < i; j++) {
2121
A[i] += A[j] * A[i - j - 1];
22-
tracer.select(j).wait();
23-
tracer.select(i - j - 1).wait();
24-
tracer.notify(i, A[i]).wait();
22+
tracer.select(j).delay();
23+
tracer.select(i - j - 1).delay();
24+
tracer.patch(i, A[i]).delay();
2525
tracer.deselect(j);
2626
tracer.deselect(i - j - 1);
27-
tracer.denotify(i);
27+
tracer.depatch(i);
2828
}
2929
}
3030

3131
logger.print(` The ${N}th Catalan Number is ${A[N]}`);
32-
tracer.select(N).wait();
32+
tracer.select(N).delay();

Dynamic Programming/Fibonacci Sequence/code.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const D = [1, 1];
66
for (let i = 2; i < index; i++) {
77
D.push(0);
88
}
9-
tracer.set(D).wait();
9+
tracer.set(D).delay();
1010

1111
for (let i = 2; i < index; i++) {
1212
D[i] = D[i - 2] + D[i - 1];
13-
tracer.select(i - 2, i - 1).wait();
14-
tracer.notify(i, D[i]).wait();
15-
tracer.denotify(i);
13+
tracer.select(i - 2, i - 1).delay();
14+
tracer.patch(i, D[i]).delay();
15+
tracer.depatch(i);
1616
tracer.deselect(i - 2, i - 1);
1717
}

Dynamic Programming/Integer Partition/code.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ for (let i = 0; i <= integer; i++) {
1111
D[i][1] = 1;
1212
for (let j = 0; j <= integer; j++) D[i][j] = 0;
1313
}
14-
tracer.set(D).wait();
14+
tracer.set(D).delay();
1515

1616
function partition(A, n, p) {
1717
if (n === 0) logger.print(`[${A.join(', ')}]`);
@@ -31,10 +31,10 @@ function integerPartition(n) {
3131
// We are allowed to use numbers from 2 to i
3232
for (let j = 1; j <= i; j++) {
3333
// Number of partitions without j number + number of partitions with max j
34-
tracer.select(i, j).wait();
34+
tracer.select(i, j).delay();
3535
D[i][j] = D[i][j - 1] + D[i - j][Math.max(j, i - j)];
36-
tracer.notify(i, j, D[i][j]).wait();
37-
tracer.denotify(i, j);
36+
tracer.patch(i, j, D[i][j]).delay();
37+
tracer.depatch(i, j);
3838
tracer.deselect(i, j);
3939
}
4040
}

Dynamic Programming/Knapsack Problem/code.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ for (let i = 0; i < N + 1; i++) {
1616
const tracer = new Array2DTracer('Knapsack Table').set(DP);
1717
const dataViewer1 = new Array1DTracer('Values').set(val);
1818
const dataViewer2 = new Array1DTracer('Weights').set(wt);
19-
const logger = new LogTracer().wait();
19+
const logger = new LogTracer().delay();
2020

2121
for (let i = 0; i <= N; i++) {
2222
for (let j = 0; j <= W; j++) {
@@ -26,12 +26,12 @@ for (let i = 0; i <= N; i++) {
2626
then the total weight in our collection is 0
2727
*/
2828
DP[i][0] = 0;
29-
tracer.notify(i, j, DP[i][j]).wait();
30-
tracer.denotify(i, j);
29+
tracer.patch(i, j, DP[i][j]).delay();
30+
tracer.depatch(i, j);
3131
} else if (wt[i - 1] <= j) { // take the current item in our collection
32-
dataViewer1.select(i - 1).wait();
33-
dataViewer2.select(i - 1).wait();
34-
tracer.select(i - 1, j).wait();
32+
dataViewer1.select(i - 1).delay();
33+
dataViewer2.select(i - 1).delay();
34+
tracer.select(i - 1, j).delay();
3535

3636
const A = val[i - 1] + DP[i - 1][j - wt[i - 1]];
3737
const B = DP[i - 1][j];
@@ -41,20 +41,20 @@ for (let i = 0; i <= N; i++) {
4141
*/
4242
if (A > B) {
4343
DP[i][j] = A;
44-
tracer.notify(i, j, DP[i][j]).wait();
44+
tracer.patch(i, j, DP[i][j]).delay();
4545
} else {
4646
DP[i][j] = B;
47-
tracer.notify(i, j, DP[i][j]).wait();
47+
tracer.patch(i, j, DP[i][j]).delay();
4848
}
4949

5050
tracer.deselect(i - 1, j);
51-
tracer.denotify(i, j);
51+
tracer.depatch(i, j);
5252
dataViewer2.deselect(i - 1);
5353
dataViewer1.deselect(i - 1);
5454
} else { // leave the current item from our collection
5555
DP[i][j] = DP[i - 1][j];
56-
tracer.notify(i, j, DP[i][j]).wait();
57-
tracer.denotify(i, j);
56+
tracer.patch(i, j, DP[i][j]).delay();
57+
tracer.depatch(i, j);
5858
}
5959
}
6060
}

Dynamic Programming/Longest Common Subsequence/code.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ for (let i = 0; i < m + 1; i++) {
1212
const tracer1 = new Array1DTracer('String 1').set(string1);
1313
const tracer2 = new Array1DTracer('String 2').set(string2);
1414
const tracer3 = new Array2DTracer('Memo Table').set(A);
15-
const logger = new LogTracer().wait();
15+
const logger = new LogTracer().delay();
1616

1717
let i;
1818
let j;
@@ -23,18 +23,18 @@ for (i = 0; i <= m; i++) {
2323
if (i === 0 || j === 0) {
2424
A[i][j] = 0;
2525
} else if (string1[i - 1] === string2[j - 1]) {
26-
tracer1.select(i - 1).wait();
27-
tracer2.select(j - 1).wait();
28-
tracer3.select(i - 1, j - 1).wait();
26+
tracer1.select(i - 1).delay();
27+
tracer2.select(j - 1).delay();
28+
tracer3.select(i - 1, j - 1).delay();
2929

3030
A[i][j] = A[i - 1][j - 1] + 1;
3131

3232
tracer1.deselect(i - 1);
3333
tracer2.deselect(j - 1);
3434
tracer3.deselect(i - 1, j - 1);
3535
} else {
36-
tracer3.select(i - 1, j).wait();
37-
tracer3.select(i, j - 1).wait();
36+
tracer3.select(i - 1, j).delay();
37+
tracer3.select(i, j - 1).delay();
3838

3939
if (A[i - 1][j] > A[i][j - 1]) {
4040
A[i][j] = A[i - 1][j];
@@ -45,19 +45,19 @@ for (i = 0; i <= m; i++) {
4545
tracer3.deselect(i - 1, j);
4646
tracer3.deselect(i, j - 1);
4747
}
48-
tracer3.notify(i, j, A[i][j]).wait();
49-
tracer3.denotify(i, j);
48+
tracer3.patch(i, j, A[i][j]).delay();
49+
tracer3.depatch(i, j);
5050
}
5151
}
5252

5353
let finalString = '';
5454
i = m;
5555
j = n;
5656
while (i >= 1 && j >= 1) {
57-
tracer3.select(i, j).wait();
57+
tracer3.select(i, j).delay();
5858
if (string1[i - 1] === string2[j - 1]) {
59-
tracer1.select(i - 1).wait();
60-
tracer2.select(j - 1).wait();
59+
tracer1.select(i - 1).delay();
60+
tracer2.select(j - 1).delay();
6161

6262
finalString = string1[i - 1] + finalString;
6363
i--;

Dynamic Programming/Longest Increasing Subsequence/code.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const tracer = new Array1DTracer();
44
const logger = new LogTracer();
55
const A = Randomize.array1D(10, { min: 0, max: 10 });
66
const LIS = new Array(A.length);
7-
tracer.set(A).wait();
7+
tracer.set(A).delay();
88

99
// Initialize LIS values for all indexes
1010
for (let i = 0; i < A.length; i++) {
@@ -17,8 +17,8 @@ for (let i = 1; i < A.length; i++) {
1717
tracer.select(i);
1818
logger.print(` LIS[${i}] = ${LIS[i]}`);
1919
for (let j = 0; j < i; j++) {
20-
tracer.notify(j).wait();
21-
tracer.denotify(j);
20+
tracer.patch(j).delay();
21+
tracer.depatch(j);
2222
if (A[i] > A[j] && LIS[i] < LIS[j] + 1) {
2323
LIS[i] = LIS[j] + 1;
2424
logger.print(` LIS[${i}] = ${LIS[i]}`);

0 commit comments

Comments
 (0)