Skip to content

Commit 5502b02

Browse files
committed
Rewrote in React.js
1 parent 9a0ae2d commit 5502b02

File tree

153 files changed

+1111
-1111
lines changed

Some content is hidden

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

153 files changed

+1111
-1111
lines changed

backtracking/knight's_tour/basic/code.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@ function knightTour(x, y, moveNum) {
77
var nextX = x + X[i];
88
var nextY = y + Y[i];
99

10-
posTracer._notify ( 0, nextX)._wait ();
11-
posTracer._notify ( 1, nextY)._wait ();
12-
posTracer._denotify (0);
13-
posTracer._denotify (1);
10+
posTracer.notify ( 0, nextX).wait ();
11+
posTracer.notify ( 1, nextY).wait ();
12+
posTracer.denotify (0);
13+
posTracer.denotify (1);
1414
/*
1515
Check if knight is still in the board
1616
Check that knight does not visit an already visited square
1717
*/
1818
if (nextX>=0 && nextX<N && nextY>=0 && nextY<N && board[nextX][nextY]===-1) {
1919
board[nextX][nextY] = moveNum;
2020

21-
logTracer._print ('Move to ' + nextX + ',' + nextY);
22-
boardTracer._notify ( nextX, nextY, moveNum)._wait();
23-
boardTracer._denotify( nextX, nextY);
24-
boardTracer._select ( nextX, nextY);
21+
logTracer.print ('Move to ' + nextX + ',' + nextY);
22+
boardTracer.notify ( nextX, nextY, moveNum).wait();
23+
boardTracer.denotify( nextX, nextY);
24+
boardTracer.select ( nextX, nextY);
2525

2626
var nextMoveNum = moveNum + 1;
2727
if ( knightTour (nextX,nextY, nextMoveNum) === true) {
2828
return true;
2929
} else {
30-
logTracer._print ('No place to move from ' + nextX + ',' +nextY + ': Backtrack');
30+
logTracer.print ('No place to move from ' + nextX + ',' +nextY + ': Backtrack');
3131
board[nextX][nextY] = -1; // backtrack
32-
boardTracer._notify ( nextX, nextY, -1)._wait();
33-
boardTracer._denotify( nextX, nextY);
34-
boardTracer._deselect( nextX, nextY);
32+
boardTracer.notify ( nextX, nextY, -1).wait();
33+
boardTracer.denotify( nextX, nextY);
34+
boardTracer.deselect( nextX, nextY);
3535
}
3636
} else {
37-
logTracer._print (nextX + ',' + nextY + ' is not a valid move');
37+
logTracer.print (nextX + ',' + nextY + ' is not a valid move');
3838
}
3939
}
4040
return false;
@@ -44,16 +44,16 @@ board[0][0] = 0; // start from this position
4444
pos[0] = 0;
4545
pos[0] = 0;
4646

47-
boardTracer._notify ( 0, 0, 0)._wait();
48-
posTracer._notify ( 0, 0)._wait ();
49-
posTracer._notify ( 1, 0)._wait ();
50-
boardTracer._denotify( 0, 0);
51-
boardTracer._denotify( 0, 0);
52-
posTracer._denotify (0);
53-
posTracer._denotify (1);
47+
boardTracer.notify ( 0, 0, 0).wait();
48+
posTracer.notify ( 0, 0).wait ();
49+
posTracer.notify ( 1, 0).wait ();
50+
boardTracer.denotify( 0, 0);
51+
boardTracer.denotify( 0, 0);
52+
posTracer.denotify (0);
53+
posTracer.denotify (1);
5454

5555
if (knightTour ( 0, 0, 1) === false ) {
56-
logTracer._print ('Solution does not exist');
56+
logTracer.print ('Solution does not exist');
5757
} else {
58-
logTracer._print ('Solution found');
58+
logTracer.print ('Solution found');
5959
}

backtracking/knight's_tour/basic/data.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ var Y = [ 1, 2, 2, 1, -1, -2, -2, -1 ];
2424
var pos = new Array (2);
2525
pos[0] = pos[1] = -1;
2626

27-
var boardTracer = new Array2DTracer ('Board')._setData (board);
28-
var posTracer = new Array1DTracer ('Knight Position')._setData (pos);
27+
var boardTracer = new Array2DTracer ('Board').set (board);
28+
var posTracer = new Array1DTracer ('Knight Position').set (pos);
2929
var logTracer = new LogTracer ('Console');

backtracking/n_queens/n_queens/code.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,40 @@ function validState (row, col, currentQueen) {
99
}
1010

1111
function nQ (currentQueen, currentCol) {
12-
logger._print ('Starting new iteration of nQueens () with currentQueen = ' + currentQueen + ' & currentCol = ' + currentCol);
13-
logger._print ('------------------------------------------------------------------');
12+
logger.print ('Starting new iteration of nQueens () with currentQueen = ' + currentQueen + ' & currentCol = ' + currentCol);
13+
logger.print ('------------------------------------------------------------------');
1414
if (currentQueen >= N) {
15-
logger._print ('The recursion has BOTTOMED OUT. All queens have been placed successfully');
15+
logger.print ('The recursion has BOTTOMED OUT. All queens have been placed successfully');
1616
return true;
1717
}
1818

1919
var found = false, row = 0;
2020
while ( (row < N) && (!found) ) {
21-
boardTracer._select (row, currentCol)._wait ();
22-
logger._print ('Trying queen ' + currentQueen + ' at row ' + row + ' & col ' + currentCol);
21+
boardTracer.select (row, currentCol).wait ();
22+
logger.print ('Trying queen ' + currentQueen + ' at row ' + row + ' & col ' + currentCol);
2323

2424
if (validState (row, currentCol, currentQueen)) {
2525
queens [currentQueen] [0] = row;
2626
queens [currentQueen] [1] = currentCol;
2727

28-
queenTracer._notify (currentQueen, 0, row)._wait ();
29-
queenTracer._notify (currentQueen, 1, currentCol)._wait ();
30-
queenTracer._denotify (currentQueen, 0)._wait ();
31-
queenTracer._denotify (currentQueen, 1)._wait ();
28+
queenTracer.notify (currentQueen, 0, row).wait ();
29+
queenTracer.notify (currentQueen, 1, currentCol).wait ();
30+
queenTracer.denotify (currentQueen, 0).wait ();
31+
queenTracer.denotify (currentQueen, 1).wait ();
3232

3333
found = nQ (currentQueen + 1, currentCol + 1);
3434
}
3535

3636
if (!found) {
37-
boardTracer._deselect (row, currentCol)._wait ();
38-
logger._print ('row ' + row + ' & col ' + currentCol + ' didn\'t work out. Going down');
37+
boardTracer.deselect (row, currentCol).wait ();
38+
logger.print ('row ' + row + ' & col ' + currentCol + ' didn\'t work out. Going down');
3939
}
4040
row++;
4141
}
4242

4343
return found;
4444
}
4545

46-
logger._print ('Starting execution');
46+
logger.print ('Starting execution');
4747
nQ (0, 0);
48-
logger._print ('DONE');
48+
logger.print ('DONE');

backtracking/n_queens/n_queens/data.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ var boardTracer = new Array2DTracer ('Board'),
1818
queenTracer = new Array2DTracer ('Queen Positions'),
1919
logger = new LogTracer ('Progress');
2020

21-
boardTracer._setData (board);
22-
queenTracer._setData (queens);
23-
logger._print ('N Queens: ' + N + 'X' + N + 'matrix, ' + N + ' queens');
21+
boardTracer.set (board);
22+
queenTracer.set (queens);
23+
logger.print ('N Queens: ' + N + 'X' + N + 'matrix, ' + N + ' queens');

cryptography/affine_cipher/basic/code.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ function encrypt(plainText) {
1616
var index = alpha.charCodeAt(0) - 'a'.charCodeAt(0);
1717
var result = ((keys.a * index) + keys.b).mod(N);
1818

19-
logger._print('Index of ' + alpha + ' = ' + index);
19+
logger.print('Index of ' + alpha + ' = ' + index);
2020

2121
result += 'a'.charCodeAt(0);
2222
return String.fromCharCode(result);
2323
}
2424

25-
logger._print('Beginning Affine Encryption');
26-
logger._print('Encryption formula: <b>((keys.a * index_of_alphabet) + keys.b) % N</b>');
27-
logger._print('keys.a=' + keys.a + ', keys.b=' + keys.b + ', N=' + N);
25+
logger.print('Beginning Affine Encryption');
26+
logger.print('Encryption formula: <b>((keys.a * index_of_alphabet) + keys.b) % N</b>');
27+
logger.print('keys.a=' + keys.a + ', keys.b=' + keys.b + ', N=' + N);
2828

2929
for (var i in plainText) {
30-
ptTracer._select(i)._wait();
31-
ptTracer._deselect(i);
30+
ptTracer.select(i).wait();
31+
ptTracer.deselect(i);
3232

3333
cypherText += cryptAlpha(plainText [i]);
3434

35-
ptTracer._notify(i, cypherText.slice(-1))._wait();
36-
ptTracer._denotify(i);
35+
ptTracer.notify(i, cypherText.slice(-1)).wait();
36+
ptTracer.denotify(i);
3737
}
3838

3939
return cypherText;
@@ -49,35 +49,35 @@ function decrypt(cypherText) {
4949
}
5050
})();
5151

52-
logger._print('a<sup>-1</sup> = ' + aInverse);
52+
logger.print('a<sup>-1</sup> = ' + aInverse);
5353

5454
function decryptAlpha(alpha) {
5555
var index = alpha.charCodeAt(0) - 'a'.charCodeAt(0);
5656
var result = (aInverse * (index - keys.b)).mod(N);
5757

58-
logger._print('Index of ' + alpha + ' = ' + index);
58+
logger.print('Index of ' + alpha + ' = ' + index);
5959

6060
result += 'a'.charCodeAt(0);
6161
return String.fromCharCode(result);
6262
}
6363

64-
logger._print('Beginning Affine Decryption');
65-
logger._print('Decryption formula: <b>(a<sup>-1</sup> * (index - keys.b)) % N</b>');
66-
logger._print('keys.b=' + keys.b + ', N=' + N);
64+
logger.print('Beginning Affine Decryption');
65+
logger.print('Decryption formula: <b>(a<sup>-1</sup> * (index - keys.b)) % N</b>');
66+
logger.print('keys.b=' + keys.b + ', N=' + N);
6767

6868
for (var i in cypherText) {
69-
ctTracer._select(i)._wait();
70-
ctTracer._deselect(i)._wait();
69+
ctTracer.select(i).wait();
70+
ctTracer.deselect(i).wait();
7171

7272
plainText += decryptAlpha(cypherText [i]);
7373

74-
ctTracer._notify(i, plainText.slice(-1))._wait();
75-
ctTracer._denotify(i)._wait();
74+
ctTracer.notify(i, plainText.slice(-1)).wait();
75+
ctTracer.denotify(i).wait();
7676
}
7777

7878
return plainText;
7979
}
8080

8181
var cipherText = encrypt(plainText);
82-
ctTracer._setData(cipherText);
82+
ctTracer.set(cipherText);
8383
decrypt(cipherText);

cryptography/affine_cipher/basic/data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ var ptTracer = new Array1DTracer('Encryption');
1515
var ctTracer = new Array1DTracer('Decryption');
1616
var logger = new LogTracer();
1717

18-
ptTracer._setData(plainText);
18+
ptTracer.set(plainText);

cryptography/caesar_cipher/basic/code.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function getNextChar(currChar, direction) {
1111
var nextPos = direction === 'up' ? getPosUp(pos) : getPosDown(pos);
1212
var nextChar = alphabet.charAt(nextPos);
1313

14-
logger._print(currChar + ' -> ' + nextChar);
14+
logger.print(currChar + ' -> ' + nextChar);
1515
return nextChar;
1616
}
1717

@@ -20,43 +20,43 @@ function cipher(str, rotation, direction, cipherTracer) {
2020

2121
for (var i = 0; i < str.length; i++) {
2222

23-
cipherTracer._wait();
23+
cipherTracer.wait();
2424

2525
var currChar = str.charAt(i);
2626
if (typeof alphabetMap[currChar] === 'number') { // don't encrpt/decrypt characters not in alphabetMap
2727
var r = rotation;
2828

29-
logger._print('Rotating ' + currChar + ' ' + direction + ' ' + rotation + ' times');
30-
cipherTracer._select(i)._wait();
29+
logger.print('Rotating ' + currChar + ' ' + direction + ' ' + rotation + ' times');
30+
cipherTracer.select(i).wait();
3131

3232
// perform given amount of rotations in the given direction
3333
while (r-- > 0) {
3434
currChar = getNextChar(currChar, direction);
35-
cipherTracer._notify(i, currChar)._wait();
35+
cipherTracer.notify(i, currChar).wait();
3636
}
3737
} else {
38-
logger._print('Ignore this character');
38+
logger.print('Ignore this character');
3939
}
4040
str = str.substring(0, i) + currChar + str.substring(i + 1);
41-
logger._print('Current result: ' + str);
41+
logger.print('Current result: ' + str);
4242
}
4343

4444
return str;
4545
}
4646

4747
function encrypt(str, rotation) {
48-
logger._print('Encrypting: ' + str);
48+
logger.print('Encrypting: ' + str);
4949
return cipher(str, rotation, 'up', encryptTracer);
5050
}
5151

5252
function decrypt(str, rotation) {
53-
logger._print('Decrypting: ' + str);
53+
logger.print('Decrypting: ' + str);
5454
return cipher(str, rotation, 'down', decryptTracer);
5555
}
5656

5757
var encrypted = encrypt(string, rotation);
58-
logger._print('Encrypted result: ' + encrypted);
58+
logger.print('Encrypted result: ' + encrypted);
5959

60-
decryptTracer._setData(encrypted);
60+
decryptTracer.set(encrypted);
6161
var decrypted = decrypt(encrypted, rotation);
62-
logger._print('Decrypted result: ' + decrypted);
62+
logger.print('Decrypted result: ' + decrypted);

cryptography/caesar_cipher/basic/data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ var encryptTracer = new Array1DTracer('Encryption');
1313
var decryptTracer = new Array1DTracer('Decryption');
1414
var logger = new LogTracer();
1515

16-
encryptTracer._setData(string);
16+
encryptTracer.set(string);
+12-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
A[0] = 1;
2-
tracer._notify ( 0, A[0] )._wait();
3-
tracer._denotify ( 0 );
2+
tracer.notify ( 0, A[0] ).wait();
3+
tracer.denotify ( 0 );
44
A[1] = 1;
5-
tracer._notify ( 1, A[1] )._wait();
6-
tracer._denotify ( 1 );
5+
tracer.notify ( 1, A[1] ).wait();
6+
tracer.denotify ( 1 );
77

88
for (var i = 2; i <= N; i++) {
99
for (var j = 0; j < i; j++) {
1010
A[i] += A[j] * A[i-j-1];
11-
tracer._select( j )._wait();
12-
tracer._select( i - j -1 )._wait();
13-
tracer._notify( i, A[i])._wait();
14-
tracer._deselect( j );
15-
tracer._deselect( i - j - 1 );
16-
tracer._denotify( i );
11+
tracer.select( j ).wait();
12+
tracer.select( i - j -1 ).wait();
13+
tracer.notify( i, A[i]).wait();
14+
tracer.deselect( j );
15+
tracer.deselect( i - j - 1 );
16+
tracer.denotify( i );
1717
}
1818
}
1919

20-
logger._print ( ' The ' + N + 'th Catalan Number is ' + A[N] );
21-
tracer._select( N )._wait();
20+
logger.print ( ' The ' + N + 'th Catalan Number is ' + A[N] );
21+
tracer.select( N ).wait();

dp/catalan_number/catalan_number/data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ for (var i = N; i >= 0; i--) {
44
A[i] = 0;
55
}
66

7-
var tracer = new Array1DTracer( ' Catalan Numbers ')._setData( A );
7+
var tracer = new Array1DTracer( ' Catalan Numbers ').set( A );
88
var logger = new LogTracer();

dp/fibonacci/basic/code.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
for (var i = 2; i < index; i++) {
22
D[i] = D[i - 2] + D[i - 1];
3-
tracer._select(i - 2, i - 1)._wait();
4-
tracer._notify(i, D[i])._wait();
5-
tracer._denotify(i);
6-
tracer._deselect(i - 2, i - 1);
3+
tracer.select(i - 2, i - 1).wait();
4+
tracer.notify(i, D[i]).wait();
5+
tracer.denotify(i);
6+
tracer.deselect(i - 2, i - 1);
77
}

dp/fibonacci/basic/data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ var D = [1, 1];
44
for (var i = 2; i < index; i++) {
55
D.push(0);
66
}
7-
tracer._setData(D);
7+
tracer.set(D);

0 commit comments

Comments
 (0)