Skip to content

Commit e265366

Browse files
committed
added chapter 03
1 parent ebc1b85 commit e265366

10 files changed

+210
-0
lines changed

chapter03/01-Stack.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Stack.js"></script>
9+
</body>
10+
</html>

chapter03/01-Stack.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function Stack() {
2+
3+
this.items = [];
4+
5+
this.push = function(element){
6+
this.items.push(element);
7+
};
8+
9+
this.pop = function(){
10+
return this.items.pop();
11+
};
12+
13+
this.peek = function(){
14+
return this.items[this.items.length-1];
15+
};
16+
17+
this.isEmpty = function(){
18+
return this.items.length == 0;
19+
};
20+
21+
this.size = function(){
22+
return this.items.length;
23+
};
24+
25+
this.print = function(){
26+
console.log(this.items.toString());
27+
};
28+
}

chapter03/02-UsingStacks.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Stack.js"></script>
9+
<script type="text/javascript" src="02-UsingStacks.js"></script>
10+
</body>
11+
</html>

chapter03/02-UsingStacks.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var stack = new Stack();
2+
console.log(stack.isEmpty()); //outputs true
3+
stack.push(5);
4+
stack.push(8);
5+
console.log(stack.peek()); // outputs 8
6+
stack.push(11);
7+
console.log(stack.size()); // outputs 3
8+
console.log(stack.isEmpty()); //outputs false
9+
stack.push(15);
10+
stack.pop();
11+
stack.pop();
12+
console.log(stack.size()); // outputs 2
13+
stack.print(); // outputs [5, 8]

chapter03/03-BalancedSymbols.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Stack.js"></script>
9+
<script type="text/javascript" src="03-BalancedSymbols.js"></script>
10+
</body>
11+
</html>

chapter03/03-BalancedSymbols.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function matches(open, close){
2+
var opens = "([{",
3+
closers = ")]}";
4+
return opens.indexOf(open) == closers.indexOf(close);
5+
}
6+
7+
function parenthesesChecker(symbols){
8+
9+
var stack = new Stack(),
10+
balanced = true,
11+
index = 0,
12+
symbol, top;
13+
14+
while (index < symbols.length && balanced){
15+
symbol = symbols.charAt(index);
16+
if (symbol == '('|| symbol == '[' || symbol == '{'){
17+
stack.push(symbol);
18+
} else {
19+
if (stack.isEmpty()){
20+
balanced = false;
21+
} else {
22+
top = stack.pop();
23+
if (!matches(top, symbol)){
24+
balanced = false;
25+
}
26+
}
27+
}
28+
index++;
29+
}
30+
if (balanced && stack.isEmpty()){
31+
return true;
32+
}
33+
return false;
34+
}
35+
36+
console.log(parenthesesChecker('{{([][])}()}'));
37+
console.log(parenthesesChecker('[{()]'));

chapter03/04-DecimalToBinary.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Stack.js"></script>
9+
<script type="text/javascript" src="04-DecimalToBinary.js"></script>
10+
</body>
11+
</html>

chapter03/04-DecimalToBinary.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//233 == 11101001
2+
//2x(10x10) + 3x(10) + 3x(1)
3+
4+
function divideBy2(decNumber){
5+
6+
var remStack = new Stack(),
7+
rem,
8+
binaryString = '';
9+
10+
while (decNumber > 0){
11+
rem = Math.floor(decNumber % 2);
12+
remStack.push(rem);
13+
decNumber = Math.floor(decNumber / 2);
14+
}
15+
16+
while (!remStack.isEmpty()){
17+
binaryString += remStack.pop().toString();
18+
}
19+
20+
return binaryString;
21+
}
22+
23+
console.log(divideBy2(233));
24+
console.log(divideBy2(10));
25+
console.log(divideBy2(1000));
26+
27+
/*
28+
The folow algorithm converts from base 10 to any base
29+
*/
30+
function baseConverter(decNumber, base){
31+
32+
var remStack = new Stack(),
33+
rem,
34+
baseString = '',
35+
digits = '0123456789ABCDEF';
36+
37+
while (decNumber > 0){
38+
rem = Math.floor(decNumber % base);
39+
remStack.push(rem);
40+
decNumber = Math.floor(decNumber / base);
41+
}
42+
43+
while (!remStack.isEmpty()){
44+
baseString += digits[remStack.pop()];
45+
}
46+
47+
return baseString;
48+
}
49+
50+
console.log(baseConverter(100345, 2));
51+
console.log(baseConverter(100345, 8));
52+
console.log(baseConverter(100345, 16));

chapter03/05-TowerOfHanoi.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Stack.js"></script>
9+
<script type="text/javascript" src="05-TowerOfHanoi.js"></script>
10+
</body>
11+
</html>

chapter03/05-TowerOfHanoi.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function towerOfHanoi(n, from, to, helper){
2+
3+
if (n > 0){
4+
towerOfHanoi(n-1, from, helper, to);
5+
to.push(from.pop());
6+
console.log('-----')
7+
console.log('Source: ' + from.items.toString());
8+
console.log('Dest: ' + to.items.toString());
9+
console.log('Helper: ' + helper.items.toString());
10+
towerOfHanoi(n-1, helper, to, from);
11+
}
12+
}
13+
14+
var source = new Stack();
15+
source.push(3);
16+
source.push(2);
17+
source.push(1);
18+
19+
var dest = new Stack();
20+
var helper = new Stack();
21+
22+
towerOfHanoi(3, source, dest, helper);
23+
24+
source.print();
25+
helper.print();
26+
dest.print();

0 commit comments

Comments
 (0)