Skip to content

Commit 5d392e6

Browse files
committedSep 30, 2017
chapter 03: [Stacks]
1 parent 8cf2152 commit 5d392e6

40 files changed

+1292
-119
lines changed
 

‎.eslintrc.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"class-methods-use-this": 0,
2222
"no-plusplus": 0,
2323
"arrow-parens": 0,
24-
"no-console": 0
24+
"no-console": 0,
25+
"import/prefer-default-export": 0,
26+
"comma-dangle": 0,
27+
"no-underscore-dangle": 0
2528
}
2629
}

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ node_modules
44
coverage
55
.nyc_output
66
coverage.lcov
7+
mochawesome-report/*
8+
dist/js/*
9+
dist/ts/*

‎README.md

+9-30
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ Learning JavaScript Data Structures and Algorithms
44
[![Build Status](https://travis-ci.org/loiane/javascript-datastructures-algorithms.svg?branch=third-edition)](https://travis-ci.org/loiane/javascript-datastructures-algorithms)
55
[![codecov](https://codecov.io/gh/loiane/javascript-datastructures-algorithms/branch/third-edition/graph/badge.svg)](https://codecov.io/gh/loiane/javascript-datastructures-algorithms)
66

7-
Source code of **Learning JavaScript Data Structures and Algorithms** book.
7+
Source code of **Learning JavaScript Data Structures and Algorithms** book, third edition.
88

99
Work in Progress.
1010

1111
## List of available chapters:
1212

1313
* 01: [JavaScript, ECMAScript and TypeScript: a quick overview](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter01)
1414
* 02: [Arrays](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter02)
15+
* 03: [Stacks](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter03)
1516

17+
## Thrid Edition Updates
18+
19+
* Algorithms using ES2015+
20+
* Creation of a Data Structure and Algorithms library that can be used in the browser or with Node.js
21+
* Algorithms tested with Mocha + Chai (test code available in `test` directory)
22+
* TypeScript version of the source code included
1623

1724
## Installing and running the book examples With Node
1825

@@ -22,36 +29,8 @@ Work in Progress.
2229
* To see the examples, run `http-server html` or `npm run serve`. Open your browser `http:\\localhost:8080` to see the book examples
2330
* Or `cd html/chapter01` and run each javascript file with node: `node 02-Variables`
2431

25-
## Running the book examples in the browser
32+
## Running the examples in the browser
2633

2734
* Right click on the html file you would like to see the examples, right click and 'Open with Chrome (or any other browser)'
2835

29-
## List of all available examples:
30-
31-
* 01: JavaScript, ECMAScript and TypeScript: a quick overview
32-
- 01-HelloWorld
33-
- 02-Variables
34-
- 03-Operators
35-
- 04-TruthyFalsy
36-
- 05-EqualsOperators
37-
- 06-ConditionalStatements
38-
- 07-Loops
39-
- 08-Functions
40-
- 10-ObjectOrientedJS
41-
- 11-ES6letconst
42-
- 12-Es6StringTemplates
43-
- 13-ES6ArrowFunctions
44-
- 14-ES6ParameterHandling
45-
- 15-ES6EnhancedObjectProperties
46-
- 16-ES6Classes
47-
- 17-TypeScript
48-
* 02: Arrays
49-
- 01-Introduction
50-
- 02-CreatingAndInitialingArrays
51-
- 03-AddingRemovingElements
52-
- 04-TwoDimensionalMultiDimensional
53-
- 05-ArrayMethods
54-
- 06-ES6Methods
55-
- 07-TypedArrays
56-
5736
Happy Coding!

‎examples/PacktDataStructuresAlgorithms.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/chapter03/01-Stack.html

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

‎examples/chapter03/01-Stack.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Stack from './../../src/js/data-structures/stack.js'; // ES2015 modules
2+
import StackArray from './../../src/js/data-structures/stack-array.js'; // ES2015 modules
3+
// const Stack = require('../../dist/js/data-structures/stack'); // for node
4+
// const Stack = stack; // older browsers - remove from html script import: type="module"
5+
6+
const stack = new Stack(); // new StackArray();
7+
8+
// using WeakMap to store Stack items we ensure true privacy
9+
// console.log(Object.getOwnPropertyNames(stack));
10+
// console.log(Object.keys(stack));
11+
12+
console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs true
13+
14+
stack.push(5);
15+
stack.push(8);
16+
17+
console.log('stack after push 5 and 8 => ', stack.toString());
18+
19+
console.log('stack.peek() => ', stack.peek()); // outputs 8
20+
21+
stack.push(11);
22+
23+
console.log('stack.size() after push 11 => ', stack.size()); // outputs 3
24+
console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs false
25+
26+
stack.push(15);
27+
28+
stack.pop();
29+
stack.pop();
30+
31+
console.log('stack.size() after push 15 and pop twice => ', stack.size()); // outputs 2

‎examples/chapter03/01-StackSymbol.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const _items = Symbol('stackItems');
2+
3+
class Stack {
4+
constructor() {
5+
this[_items] = [];
6+
}
7+
8+
push(element) {
9+
this[_items].push(element);
10+
}
11+
12+
pop() {
13+
return this[_items].pop();
14+
}
15+
16+
peek() {
17+
return this[_items][this[_items].length - 1];
18+
}
19+
20+
isEmpty() {
21+
return this[_items].length === 0;
22+
}
23+
24+
size() {
25+
return this[_items].length;
26+
}
27+
28+
clear() {
29+
this[_items] = [];
30+
}
31+
32+
print() {
33+
console.log(this.toString());
34+
}
35+
36+
toString() {
37+
return this[_items].toString();
38+
}
39+
}
40+
41+
const stack = new Stack();
42+
const objectSymbols = Object.getOwnPropertySymbols(stack);
43+
console.log(objectSymbols.length); // 1
44+
console.log(objectSymbols); // [Symbol()]
45+
console.log(objectSymbols[0]); // Symbol()
46+
stack[objectSymbols[0]].push(1);
47+
stack.print(); // 5, 8, 1
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title></title>
7+
</head>
8+
9+
<body>
10+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
11+
<script src="02-BalancedSymbols.js"></script>
12+
</body>
13+
14+
</html>
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const parenthesesChecker = PacktDataStructuresAlgorithms.parenthesesChecker;
2+
3+
console.log('{([])}', parenthesesChecker('{([])}')); // true
4+
console.log('{{([][])}()}', parenthesesChecker('{{([][])}()}')); // true
5+
console.log('[{()]', parenthesesChecker('[{()]')); // false
+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 src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="03-DecimalToBinary.js"></script>
10+
</body>
11+
</html>
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const decimalToBinary = PacktDataStructuresAlgorithms.parenthesesChecker;
2+
const baseConverter = PacktDataStructuresAlgorithms.baseConverter;
3+
4+
// 233 == 11101001
5+
// 2x(10x10) + 3x(10) + 3x(1)
6+
console.log(decimalToBinary(233));
7+
console.log(decimalToBinary(10));
8+
console.log(decimalToBinary(1000));
9+
10+
console.log(baseConverter(100345, 2));
11+
console.log(baseConverter(100345, 8));
12+
console.log(baseConverter(100345, 16));
+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 src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="04-TowerOfHanoi.js"></script>
10+
</body>
11+
</html>

‎examples/chapter03/04-TowerOfHanoi.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const hanoiStack = PacktDataStructuresAlgorithms.hanoiStack;
2+
const hanoi = PacktDataStructuresAlgorithms.hanoi;
3+
4+
console.log(hanoiStack(3));
5+
6+
console.log(hanoi(3, 'source', 'helper', 'dest'));

‎examples/chapter03/index.html

-11
This file was deleted.

‎examples/chapter03/index.js

-8
This file was deleted.

‎examples/index.html

+15-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
<a href="#scroll-tab-1" class="mdl-layout__tab is-active">01</a>
3939
<a href="#scroll-tab-2" class="mdl-layout__tab">02</a>
4040
<a href="#scroll-tab-3" class="mdl-layout__tab">03</a>
41-
<a href="#scroll-tab-3" class="mdl-layout__tab">04</a>
42-
<a href="#scroll-tab-3" class="mdl-layout__tab">05</a>
43-
<a href="#scroll-tab-3" class="mdl-layout__tab">06</a>
41+
<a href="#scroll-tab-4" class="mdl-layout__tab">04</a>
42+
<a href="#scroll-tab-4" class="mdl-layout__tab">05</a>
43+
<a href="#scroll-tab-4" class="mdl-layout__tab">06</a>
4444
</div>
4545
</header>
4646
<main class="mdl-layout__content">
@@ -95,9 +95,20 @@
9595
</section>
9696
<section class="mdl-layout__tab-panel" id="scroll-tab-3">
9797
<div class="page-content">
98-
Soon.
98+
<div class="page-content mdl-layout--fixed-drawer">
99+
<div class="mdl-layout__drawer is-visible">
100+
<nav class="mdl-navigation">
101+
<a class="mdl-navigation__link" href="./chapter03/01-Stack.html">01-Stack</a>
102+
</nav>
103+
</div>
104+
</div>
99105
</div>
100106
</section>
107+
<section class="mdl-layout__tab-panel" id="scroll-tab-4">
108+
<div class="page-content">
109+
Soon.
110+
</div>
111+
</section>
101112
</main>
102113
</div>
103114
</body>

‎package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@
1414
},
1515
"homepage": "https://github.com/loiane/javascript-datastructures-algorithms",
1616
"scripts": {
17-
"clean": "rm -rf ./dist ./coverage ./.nyc_output ./coverage.lcov",
17+
"clean": "rm -rf ./dist ./coverage ./.nyc_output ./coverage.lcov ./mochawesome-report",
1818
"build:js": "babel src/js -d dist/js",
1919
"build:ts": "tsc -p ./ --rootDir ./src/ts",
2020
"build": "npm run build:js && npm run build:ts",
2121
"lint:js": "eslint src/js && eslint test/js",
2222
"lint:ts": "tslint -c tslint.json 'src/ts/**/*.ts' && tslint -c tslint.json 'test/ts/**/*.ts'",
2323
"lint": "npm run lint:js && npm run lint:ts",
24-
"test:js": "mocha --compilers js:babel-core/register ./test/js/**/*.spec.js",
24+
"test:js": "mocha --compilers js:babel-core/register ./test/js/**/*.spec.js --reporter mochawesome",
2525
"test:ts": "mocha -r ts-node/register --recursive ./test/ts/**/*.spec.ts",
2626
"test": "npm run test:js && npm run test:ts",
2727
"dev": "npm run clean && npm run lint && npm run generate-report",
2828
"coverage": "npm run generate-report && nyc report --reporter=text-lcov > coverage.lcov && codecov",
2929
"generate-report": "nyc --report-dir coverage npm run test && nyc report --reporter=text",
30-
"go": "npm run clean && npm run lint && npm run build && npm run test"
30+
"go": "npm run clean && npm run lint && npm run build && npm run test",
31+
"webpack": "webpack --env build"
3132
},
3233
"nyc": {
3334
"include": [
@@ -55,6 +56,7 @@
5556
"babel-cli": "^6.26.0",
5657
"babel-core": "^6.26.0",
5758
"babel-eslint": "^8.0.0",
59+
"babel-loader": "^7.1.2",
5860
"babel-plugin-add-module-exports": "^0.2.1",
5961
"babel-plugin-transform-es2015-modules-umd": "^6.24.1",
6062
"babel-preset-env": "^1.6.0",
@@ -65,9 +67,12 @@
6567
"eslint-plugin-import": "^2.7.0",
6668
"istanbul": "^v1.1.0-alpha.1",
6769
"mocha": "^3.5.0",
70+
"mochawesome": "^2.3.1",
6871
"nyc": "^11.2.1",
6972
"ts-node": "^3.3.0",
7073
"tslint": "^5.7.0",
71-
"typescript": "^2.5.2"
74+
"typescript": "^2.5.2",
75+
"webpack": "^3.6.0",
76+
"yargs": "^9.0.1"
7277
}
7378
}

‎src/js/data-structures/stack-array.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// @ts-check
2+
3+
export default class StackArray {
4+
constructor() {
5+
this._items = [];
6+
}
7+
push(element) {
8+
this._items.push(element);
9+
}
10+
11+
pop() {
12+
return this._items.pop();
13+
}
14+
15+
peek() {
16+
return this._items[this._items.length - 1];
17+
}
18+
19+
isEmpty() {
20+
return this._items.length === 0;
21+
}
22+
23+
size() {
24+
return this._items.length;
25+
}
26+
27+
clear() {
28+
this._items = [];
29+
}
30+
31+
toArray() {
32+
return this._items;
33+
}
34+
35+
toString() {
36+
return this._items.toString();
37+
}
38+
}

‎src/js/data-structures/stack.js

+44-15
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,70 @@
11
// @ts-check
22

3-
let items;
3+
4+
const _items = new WeakMap();
5+
const _count = new WeakMap();
6+
47
export default class Stack {
58
constructor() {
6-
items = [];
9+
_count.set(this, 0);
10+
_items.set(this, {});
711
}
12+
813
push(element) {
9-
items.push(element);
14+
const items = _items.get(this);
15+
const count = _count.get(this);
16+
items[count] = element;
17+
_count.set(this, count + 1);
1018
}
1119

1220
pop() {
13-
return items.pop();
21+
if (this.isEmpty()) {
22+
return undefined;
23+
}
24+
const items = _items.get(this);
25+
let count = _count.get(this);
26+
count--;
27+
_count.set(this, count);
28+
const result = items[count];
29+
delete items[count];
30+
return result;
1431
}
1532

1633
peek() {
17-
return items[items.length - 1];
34+
if (this.isEmpty()) {
35+
return undefined;
36+
}
37+
const items = _items.get(this);
38+
const count = _count.get(this);
39+
return items[count - 1];
1840
}
1941

2042
isEmpty() {
21-
return items.length === 0;
43+
return _count.get(this) === 0;
2244
}
2345

2446
size() {
25-
return items.length;
47+
return _count.get(this);
2648
}
2749

2850
clear() {
29-
items = [];
30-
}
31-
32-
toArray() {
33-
return items;
51+
/* while (!this.isEmpty()) {
52+
this.pop();
53+
} */
54+
_count.set(this, 0);
55+
_items.set(this, {});
3456
}
3557

3658
toString() {
37-
return items.toString();
59+
if (this.isEmpty()) {
60+
return '';
61+
}
62+
const items = _items.get(this);
63+
const count = _count.get(this);
64+
let objString = `${items[0]}`;
65+
for (let i = 1; i < count; i++) {
66+
objString = `${objString},${items[i]}`;
67+
}
68+
return objString;
3869
}
3970
}
40-
41-
// export default Stack;

‎src/js/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { hanoi, hanoiStack } from './others/hanoi';
2+
import { baseConverter, decimalToBinary } from './others/base-converter';
3+
import StackArray from './data-structures/stack-array';
4+
import Stack from './data-structures/stack';
5+
import { parenthesesChecker } from './others/balanced-symbols';
6+
7+
export {
8+
Stack,
9+
StackArray,
10+
parenthesesChecker,
11+
baseConverter,
12+
decimalToBinary,
13+
hanoi,
14+
hanoiStack
15+
};

‎src/js/others/balanced-symbols.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @ts-check
2+
import Stack from '../data-structures/stack';
3+
4+
export function parenthesesChecker(symbols) {
5+
const stack = new Stack();
6+
const opens = '([{';
7+
const closers = ')]}';
8+
let balanced = true;
9+
let index = 0;
10+
let symbol;
11+
let top;
12+
13+
while (index < symbols.length && balanced) {
14+
symbol = symbols.charAt(index);
15+
if (opens.indexOf(symbol) >= 0) {
16+
stack.push(symbol);
17+
} else if (stack.isEmpty()) {
18+
balanced = false;
19+
} else {
20+
top = stack.pop();
21+
if (!(opens.indexOf(top) === closers.indexOf(symbol))) {
22+
balanced = false;
23+
}
24+
}
25+
index++;
26+
}
27+
if (balanced && stack.isEmpty()) {
28+
return true;
29+
}
30+
return false;
31+
}

‎src/js/others/base-converter.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @ts-check
2+
import Stack from '../data-structures/stack';
3+
4+
export function decimalToBinary(decNumber) {
5+
const remStack = new Stack();
6+
let number = decNumber;
7+
let rem;
8+
let binaryString = '';
9+
10+
while (number > 0) {
11+
rem = Math.floor(number % 2);
12+
remStack.push(rem);
13+
number = Math.floor(number / 2);
14+
}
15+
16+
while (!remStack.isEmpty()) {
17+
binaryString += remStack.pop().toString();
18+
}
19+
20+
return binaryString;
21+
}
22+
23+
export function baseConverter(decNumber, base) {
24+
const remStack = new Stack();
25+
const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
26+
let number = decNumber;
27+
let rem;
28+
let baseString = '';
29+
30+
if (!(base >= 2 && base <= 36)) {
31+
return '';
32+
}
33+
34+
while (number > 0) {
35+
rem = Math.floor(number % base);
36+
remStack.push(rem);
37+
number = Math.floor(number / base);
38+
}
39+
40+
while (!remStack.isEmpty()) {
41+
baseString += digits[remStack.pop()];
42+
}
43+
44+
return baseString;
45+
}

‎src/js/others/hanoi.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// @ts-check
2+
import Stack from '../data-structures/stack';
3+
4+
function towerOfHanoi(plates, source, helper, dest, sourceName, helperName, destName, moves = []) {
5+
if (plates <= 0) {
6+
return moves;
7+
}
8+
if (plates === 1) {
9+
dest.push(source.pop());
10+
const move = {};
11+
move[sourceName] = source.toString();
12+
move[helperName] = helper.toString();
13+
move[destName] = dest.toString();
14+
moves.push(move);
15+
} else {
16+
towerOfHanoi(plates - 1, source, dest, helper, sourceName, destName, helperName, moves);
17+
dest.push(source.pop());
18+
const move = {};
19+
move[sourceName] = source.toString();
20+
move[helperName] = helper.toString();
21+
move[destName] = dest.toString();
22+
moves.push(move);
23+
towerOfHanoi(plates - 1, helper, source, dest, helperName, sourceName, destName, moves);
24+
}
25+
return moves;
26+
}
27+
28+
export function hanoiStack(plates) {
29+
const source = new Stack();
30+
const dest = new Stack();
31+
const helper = new Stack();
32+
33+
for (let i = plates; i > 0; i--) {
34+
source.push(i);
35+
}
36+
37+
return towerOfHanoi(plates, source, helper, dest, 'source', 'helper', 'dest');
38+
}
39+
40+
export function hanoi(plates, source, helper, dest, moves = []) {
41+
if (plates <= 0) {
42+
return moves;
43+
}
44+
if (plates === 1) {
45+
moves.push([source, dest]);
46+
} else {
47+
hanoi(plates - 1, source, dest, helper, moves);
48+
moves.push([source, dest]);
49+
hanoi(plates - 1, helper, source, dest, moves);
50+
}
51+
return moves;
52+
}

‎src/ts/data-structures/stack-array.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export default class StackArray<T> {
2+
private items: T[];
3+
4+
constructor() {
5+
this.items = [];
6+
}
7+
8+
push(element: T) {
9+
this.items.push(element);
10+
}
11+
12+
pop() {
13+
return this.items.pop();
14+
}
15+
16+
peek() {
17+
return this.items[this.items.length - 1];
18+
}
19+
20+
isEmpty() {
21+
return this.items.length === 0;
22+
}
23+
24+
size() {
25+
return this.items.length;
26+
}
27+
28+
clear() {
29+
this.items = [];
30+
}
31+
32+
toArray() {
33+
return this.items;
34+
}
35+
36+
toString() {
37+
return this.items.toString();
38+
}
39+
}

‎src/ts/data-structures/stack.ts

+32-12
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,59 @@
11
export default class Stack<T> {
2-
private items: T[];
2+
private count: number;
3+
private items: any;
34

45
constructor() {
5-
this.items = [];
6+
this.count = 0;
7+
this.items = {};
68
}
79

810
push(element: T) {
9-
this.items.push(element);
11+
this.items[this.count] = element;
12+
this.count++;
1013
}
1114

1215
pop() {
13-
return this.items.pop();
16+
if (this.isEmpty()) {
17+
return undefined;
18+
}
19+
this.count--;
20+
const result = this.items[this.count];
21+
delete this.items[this.count];
22+
return result;
1423
}
1524

1625
peek() {
17-
return this.items[this.items.length - 1];
26+
if (this.isEmpty()) {
27+
return undefined;
28+
}
29+
return this.items[this.count - 1];
1830
}
1931

2032
isEmpty() {
21-
return this.items.length === 0;
33+
return this.count === 0;
2234
}
2335

2436
size() {
25-
return this.items.length;
37+
return this.count;
2638
}
2739

2840
clear() {
29-
this.items = [];
30-
}
41+
/* while (!this.isEmpty()) {
42+
this.pop();
43+
} */
3144

32-
toArray() {
33-
return this.items;
45+
this.items = {};
46+
this.count = 0;
3447
}
3548

3649
toString() {
37-
return this.items.toString();
50+
if (this.isEmpty()) {
51+
return '';
52+
}
53+
let objString = `${this.items[0]}`;
54+
for (let i = 1; i < this.count; i++) {
55+
objString = `${objString},${this.items[i]}`;
56+
}
57+
return objString;
3858
}
3959
}

‎src/ts/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { hanoi, hanoiStack } from './others/hanoi';
2+
import { baseConverter, decimalToBinary } from './others/base-converter';
3+
import StackArray from './data-structures/stack-array';
4+
import Stack from './data-structures/stack';
5+
import { parenthesesChecker } from './others/balanced-symbols';
6+
7+
export {
8+
Stack,
9+
StackArray,
10+
parenthesesChecker,
11+
baseConverter,
12+
decimalToBinary,
13+
hanoi,
14+
hanoiStack
15+
};

‎src/ts/others/balanced-symbols.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Stack from '../data-structures/stack';
2+
3+
export function parenthesesChecker(symbols: string) {
4+
const stack = new Stack<string>();
5+
const opens = '([{';
6+
const closers = ')]}';
7+
let balanced = true;
8+
let index = 0;
9+
let symbol: string;
10+
let top: string;
11+
12+
while (index < symbols.length && balanced) {
13+
symbol = symbols.charAt(index);
14+
if (opens.indexOf(symbol) >= 0) {
15+
stack.push(symbol);
16+
// console.log(`open symbol - stacking ${symbol}`);
17+
} else {
18+
// console.log(`close symbol ${symbol}`);
19+
if (stack.isEmpty()) {
20+
balanced = false;
21+
// console.log('Stack is empty, no more symbols to pop and compare');
22+
} else {
23+
top = stack.pop();
24+
// if (!matches(top, symbol)){
25+
if (!(opens.indexOf(top) === closers.indexOf(symbol))) {
26+
balanced = false;
27+
/* console.log(
28+
`poping symbol ${top} - is not a match compared to ${symbol}`
29+
); */
30+
} /* else {
31+
console.log(
32+
`poping symbol ${top} - is is a match compared to ${symbol}`
33+
);
34+
} */
35+
}
36+
}
37+
index++;
38+
}
39+
if (balanced && stack.isEmpty()) {
40+
return true;
41+
}
42+
return false;
43+
}

‎src/ts/others/base-converter.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Stack from '../data-structures/stack';
2+
3+
export function decimalToBinary(decNumber: number) {
4+
const remStack = new Stack();
5+
let rem: number;
6+
let binaryString = '';
7+
8+
while (decNumber > 0) {
9+
rem = Math.floor(decNumber % 2);
10+
remStack.push(rem);
11+
decNumber = Math.floor(decNumber / 2);
12+
}
13+
14+
while (!remStack.isEmpty()) {
15+
binaryString += remStack.pop().toString();
16+
}
17+
18+
return binaryString;
19+
}
20+
21+
export function baseConverter(decNumber: number, base: number) {
22+
const remStack = new Stack();
23+
const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
24+
let rem: number;
25+
let baseString = '';
26+
27+
if (!(base >= 2 && base <= 36)) {
28+
return '';
29+
}
30+
31+
while (decNumber > 0) {
32+
rem = Math.floor(decNumber % base);
33+
remStack.push(rem);
34+
decNumber = Math.floor(decNumber / base);
35+
}
36+
37+
while (!remStack.isEmpty()) {
38+
baseString += digits[remStack.pop()];
39+
}
40+
41+
return baseString;
42+
}

‎src/ts/others/hanoi.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Stack from '../data-structures/stack';
2+
3+
function towerOfHanoi(
4+
plates: number,
5+
source: Stack<number>,
6+
helper: Stack<number>,
7+
dest: Stack<number>,
8+
sourceName: string, helperName: string, destName: string,
9+
moves: any[] = []
10+
) {
11+
if (plates <= 0) {
12+
return moves;
13+
}
14+
if (plates === 1) {
15+
dest.push(source.pop());
16+
const move: any = {};
17+
move[sourceName] = source.toString();
18+
move[helperName] = helper.toString();
19+
move[destName] = dest.toString();
20+
moves.push(move);
21+
} else {
22+
towerOfHanoi(plates - 1, source, dest, helper, sourceName, destName, helperName, moves);
23+
dest.push(source.pop());
24+
const move: any = {};
25+
move[sourceName] = source.toString();
26+
move[helperName] = helper.toString();
27+
move[destName] = dest.toString();
28+
moves.push(move);
29+
towerOfHanoi(plates - 1, helper, source, dest, helperName, sourceName, destName, moves);
30+
}
31+
return moves;
32+
}
33+
34+
export function hanoiStack(plates: number) {
35+
const source = new Stack<number>();
36+
const dest = new Stack<number>();
37+
const helper = new Stack<number>();
38+
39+
for (let i = plates; i > 0; i--) {
40+
source.push(i);
41+
}
42+
43+
return towerOfHanoi(plates, source, helper, dest, 'source', 'helper', 'dest');
44+
}
45+
46+
export function hanoi(
47+
plates: number,
48+
source: string,
49+
helper: string,
50+
dest: string,
51+
moves: string[][] = []
52+
) {
53+
if (plates <= 0) {
54+
return moves;
55+
}
56+
if (plates === 1) {
57+
moves.push([source, dest]);
58+
} else {
59+
hanoi(plates - 1, source, dest, helper, moves);
60+
moves.push([source, dest]);
61+
hanoi(plates - 1, helper, source, dest, moves);
62+
}
63+
return moves;
64+
}
+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import StackArray from '../../../src/js/data-structures/stack-array';
4+
5+
describe('StackArray', () => {
6+
let stack;
7+
8+
beforeEach(() => {
9+
stack = new StackArray();
10+
});
11+
12+
it('starts empty', () => {
13+
expect(stack.size()).to.equal(0);
14+
expect(stack.isEmpty()).to.equal(true);
15+
});
16+
17+
it('pushes elements', () => {
18+
stack.push(1);
19+
expect(stack.size()).to.equal(1);
20+
stack.push(2);
21+
expect(stack.size()).to.equal(2);
22+
stack.push(3);
23+
expect(stack.size()).to.equal(3);
24+
25+
expect(stack.isEmpty()).to.equal(false);
26+
});
27+
28+
it('pops elements', () => {
29+
stack.push(1);
30+
stack.push(2);
31+
stack.push(3);
32+
33+
expect(stack.pop()).to.equal(3);
34+
expect(stack.pop()).to.equal(2);
35+
expect(stack.pop()).to.equal(1);
36+
expect(stack.pop()).to.equal(undefined);
37+
});
38+
39+
it('implements LIFO logic', () => {
40+
stack.push(1);
41+
stack.push(2);
42+
stack.push(3);
43+
44+
expect(stack.pop()).to.equal(3);
45+
expect(stack.pop()).to.equal(2);
46+
expect(stack.pop()).to.equal(1);
47+
expect(stack.pop()).to.equal(undefined);
48+
});
49+
50+
it('allows to peek at the top element in he stack without popping it', () => {
51+
expect(stack.peek()).to.equal(undefined);
52+
53+
stack.push(1);
54+
expect(stack.peek()).to.equal(1);
55+
56+
stack.push(2);
57+
expect(stack.peek()).to.equal(2);
58+
59+
stack.pop();
60+
expect(stack.peek()).to.equal(1);
61+
});
62+
63+
it('returns the correct size', () => {
64+
expect(stack.size()).to.equal(0);
65+
stack.push(1);
66+
expect(stack.size()).to.equal(1);
67+
stack.push(2);
68+
expect(stack.size()).to.equal(2);
69+
stack.push(3);
70+
expect(stack.size()).to.equal(3);
71+
72+
stack.clear();
73+
expect(stack.isEmpty()).to.equal(true);
74+
75+
stack.push(1);
76+
stack.push(2);
77+
stack.push(3);
78+
79+
stack.pop();
80+
expect(stack.size()).to.equal(2);
81+
stack.pop();
82+
expect(stack.size()).to.equal(1);
83+
stack.pop();
84+
expect(stack.size()).to.equal(0);
85+
stack.pop();
86+
expect(stack.size()).to.equal(0);
87+
});
88+
89+
it('returns if it is empty', () => {
90+
expect(stack.isEmpty()).to.equal(true);
91+
stack.push(1);
92+
expect(stack.isEmpty()).to.equal(false);
93+
stack.push(2);
94+
expect(stack.isEmpty()).to.equal(false);
95+
stack.push(3);
96+
expect(stack.isEmpty()).to.equal(false);
97+
98+
stack.clear();
99+
expect(stack.isEmpty()).to.equal(true);
100+
101+
stack.push(1);
102+
stack.push(2);
103+
stack.push(3);
104+
105+
stack.pop();
106+
expect(stack.isEmpty()).to.equal(false);
107+
stack.pop();
108+
expect(stack.isEmpty()).to.equal(false);
109+
stack.pop();
110+
expect(stack.isEmpty()).to.equal(true);
111+
stack.pop();
112+
expect(stack.isEmpty()).to.equal(true);
113+
});
114+
115+
it('clears the stack', () => {
116+
stack.clear();
117+
expect(stack.isEmpty()).to.equal(true);
118+
119+
stack.push(1);
120+
stack.push(2);
121+
122+
stack.clear();
123+
expect(stack.isEmpty()).to.equal(true);
124+
});
125+
126+
it('returns an Array', () => {
127+
let stackArray = stack.toArray();
128+
expect(stackArray.length).to.equal(0);
129+
130+
stack.push(1);
131+
stack.push(2);
132+
133+
stackArray = stack.toArray();
134+
expect(stackArray.length).to.equal(2);
135+
136+
let i = 1;
137+
stackArray.forEach(e => {
138+
expect(e).to.equal(i);
139+
i++;
140+
});
141+
});
142+
143+
it('returns toString primitive types', () => {
144+
expect(stack.toString()).to.equal('');
145+
146+
stack.push(1);
147+
expect(stack.toString()).to.equal('1');
148+
149+
stack.push(2);
150+
expect(stack.toString()).to.equal('1,2');
151+
152+
stack.clear();
153+
expect(stack.toString()).to.equal('');
154+
155+
stack.push('el1');
156+
expect(stack.toString()).to.equal('el1');
157+
158+
stack.push('el2');
159+
expect(stack.toString()).to.equal('el1,el2');
160+
});
161+
162+
it('returns toString objects', () => {
163+
class MyObj {
164+
constructor(el1, el2) {
165+
this.el1 = el1;
166+
this.el2 = el2;
167+
}
168+
toString() {
169+
return `${this.el1.toString()}|${this.el2.toString()}`;
170+
}
171+
}
172+
expect(stack.toString()).to.equal('');
173+
174+
stack.push(new MyObj(1, 2));
175+
expect(stack.toString()).to.equal('1|2');
176+
177+
stack.push(new MyObj(3, 4));
178+
expect(stack.toString()).to.equal('1|2,3|4');
179+
});
180+
});

‎test/js/data-structures/stack.spec.js

-17
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,6 @@ describe('Stack', () => {
123123
expect(stack.isEmpty()).to.equal(true);
124124
});
125125

126-
it('returns an Array', () => {
127-
let stackArray = stack.toArray();
128-
expect(stackArray.length).to.equal(0);
129-
130-
stack.push(1);
131-
stack.push(2);
132-
133-
stackArray = stack.toArray();
134-
expect(stackArray.length).to.equal(2);
135-
136-
let i = 1;
137-
stackArray.forEach(e => {
138-
expect(e).to.equal(i);
139-
i++;
140-
});
141-
});
142-
143126
it('returns toString primitive types', () => {
144127
expect(stack.toString()).to.equal('');
145128

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { parenthesesChecker } from '../../../src/js/others/balanced-symbols';
4+
5+
describe('Balanced Symbols', () => {
6+
it('empty to be falsy', () => {
7+
expect(parenthesesChecker('')).to.equal(true);
8+
});
9+
10+
it('{ to be falsy', () => {
11+
expect(parenthesesChecker('{')).to.equal(false);
12+
});
13+
14+
it('} to be falsy', () => {
15+
expect(parenthesesChecker('}')).to.equal(false);
16+
});
17+
18+
it('11 to be falsy', () => {
19+
expect(parenthesesChecker('11')).to.equal(false);
20+
});
21+
22+
it('{11 to be falsy', () => {
23+
expect(parenthesesChecker('{11')).to.equal(false);
24+
});
25+
26+
it('{([1])} to be falsy', () => {
27+
expect(parenthesesChecker('{([1])}')).to.equal(false);
28+
});
29+
30+
it('{([])} to be truthy', () => {
31+
expect(parenthesesChecker('{([])}')).to.equal(true);
32+
});
33+
34+
it('{{([][])}()} to be truthy', () => {
35+
expect(parenthesesChecker('{{([][])}()}')).to.equal(true);
36+
});
37+
38+
it('[{()] to be falsy', () => {
39+
expect(parenthesesChecker('[{()]')).to.equal(false);
40+
});
41+
});

‎test/js/others/base-converter.spec.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { baseConverter, decimalToBinary } from '../../../src/js/others/base-converter';
4+
5+
describe('Base Converter', () => {
6+
it('decimalToBinary 1 -> 1', () => {
7+
expect(decimalToBinary(1)).to.equal('1');
8+
});
9+
10+
it('decimalToBinary 2 -> 11', () => {
11+
expect(decimalToBinary(2)).to.equal('10');
12+
});
13+
14+
it('decimalToBinary 233 -> 11101001', () => {
15+
expect(decimalToBinary(233)).to.equal('11101001');
16+
});
17+
18+
it('decimalToBinary 10 -> 1010', () => {
19+
expect(decimalToBinary(10)).to.equal('1010');
20+
});
21+
22+
it('decimalToBinary 1000 -> 1111101000', () => {
23+
expect(decimalToBinary(1000)).to.equal('1111101000');
24+
});
25+
26+
it('baseConverter 100345, 2 -> 11000011111111001', () => {
27+
expect(baseConverter(100345, 2)).to.equal('11000011111111001');
28+
});
29+
30+
it('baseConverter 100345, 8 -> 303771', () => {
31+
expect(baseConverter(100345, 8)).to.equal('303771');
32+
});
33+
34+
it('baseConverter 100345, 16 -> 187F9', () => {
35+
expect(baseConverter(100345, 16)).to.equal('187F9');
36+
});
37+
38+
it('baseConverter 100345, 7 -> 565360', () => {
39+
expect(baseConverter(100345, 7)).to.equal('565360');
40+
});
41+
42+
it('baseConverter 100345, 20 -> CAH5', () => {
43+
expect(baseConverter(100345, 20)).to.equal('CAH5');
44+
});
45+
46+
it('baseConverter 100345, 35 -> 2BW0', () => {
47+
expect(baseConverter(100345, 35)).to.equal('2BW0');
48+
});
49+
50+
it('baseConverter 100345, 36 -> 25FD', () => {
51+
expect(baseConverter(100345, 36)).to.equal('25FD');
52+
});
53+
54+
it('baseConverter 100345, 37 -> ', () => {
55+
expect(baseConverter(100345, 37)).to.equal('');
56+
});
57+
});

‎test/js/others/hanoi.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { hanoi, hanoiStack } from '../../../src/js/others/hanoi';
4+
5+
describe('Tower of Hanoi', () => {
6+
it('Hanoi', () => {
7+
for (let i = 0; i < 10; i++) {
8+
const result = hanoi(i, 'a', 'b', 'c');
9+
expect(result.length).to.equal((2 ** i) - 1);
10+
}
11+
});
12+
13+
it('Hanoi with Stack', () => {
14+
for (let i = 0; i < 10; i++) {
15+
const result = hanoiStack(i);
16+
expect(result.length).to.equal((2 ** i) - 1);
17+
}
18+
});
19+
});
+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import StackArray from '../../../src/ts/data-structures/stack-array';
4+
5+
describe('StackArray', () => {
6+
let stack: StackArray<number>;
7+
8+
beforeEach(function() {
9+
stack = new StackArray<number>();
10+
});
11+
12+
it('starts empty', () => {
13+
expect(stack.size()).to.equal(0);
14+
expect(stack.isEmpty()).to.equal(true);
15+
});
16+
17+
it('pushes elements', () => {
18+
stack.push(1);
19+
expect(stack.size()).to.equal(1);
20+
stack.push(2);
21+
expect(stack.size()).to.equal(2);
22+
stack.push(3);
23+
expect(stack.size()).to.equal(3);
24+
25+
expect(stack.isEmpty()).to.equal(false);
26+
});
27+
28+
it('pops elements', () => {
29+
stack.push(1);
30+
stack.push(2);
31+
stack.push(3);
32+
33+
expect(stack.pop()).to.equal(3);
34+
expect(stack.pop()).to.equal(2);
35+
expect(stack.pop()).to.equal(1);
36+
expect(stack.pop()).to.equal(undefined);
37+
});
38+
39+
it('implements LIFO logic', () => {
40+
stack.push(1);
41+
stack.push(2);
42+
stack.push(3);
43+
44+
expect(stack.pop()).to.equal(3);
45+
expect(stack.pop()).to.equal(2);
46+
expect(stack.pop()).to.equal(1);
47+
expect(stack.pop()).to.equal(undefined);
48+
});
49+
50+
it('allows to peek at the top element in he stack without popping it', () => {
51+
expect(stack.peek()).to.equal(undefined);
52+
53+
stack.push(1);
54+
expect(stack.peek()).to.equal(1);
55+
56+
stack.push(2);
57+
expect(stack.peek()).to.equal(2);
58+
59+
stack.pop();
60+
expect(stack.peek()).to.equal(1);
61+
});
62+
63+
it('returns the correct size', () => {
64+
expect(stack.size()).to.equal(0);
65+
stack.push(1);
66+
expect(stack.size()).to.equal(1);
67+
stack.push(2);
68+
expect(stack.size()).to.equal(2);
69+
stack.push(3);
70+
expect(stack.size()).to.equal(3);
71+
72+
stack.clear();
73+
expect(stack.isEmpty()).to.equal(true);
74+
75+
stack.push(1);
76+
stack.push(2);
77+
stack.push(3);
78+
79+
stack.pop();
80+
expect(stack.size()).to.equal(2);
81+
stack.pop();
82+
expect(stack.size()).to.equal(1);
83+
stack.pop();
84+
expect(stack.size()).to.equal(0);
85+
stack.pop();
86+
expect(stack.size()).to.equal(0);
87+
});
88+
89+
it('returns if it is empty', () => {
90+
expect(stack.isEmpty()).to.equal(true);
91+
stack.push(1);
92+
expect(stack.isEmpty()).to.equal(false);
93+
stack.push(2);
94+
expect(stack.isEmpty()).to.equal(false);
95+
stack.push(3);
96+
expect(stack.isEmpty()).to.equal(false);
97+
98+
stack.clear();
99+
expect(stack.isEmpty()).to.equal(true);
100+
101+
stack.push(1);
102+
stack.push(2);
103+
stack.push(3);
104+
105+
stack.pop();
106+
expect(stack.isEmpty()).to.equal(false);
107+
stack.pop();
108+
expect(stack.isEmpty()).to.equal(false);
109+
stack.pop();
110+
expect(stack.isEmpty()).to.equal(true);
111+
stack.pop();
112+
expect(stack.isEmpty()).to.equal(true);
113+
});
114+
115+
it('clears the stack', () => {
116+
stack.clear();
117+
expect(stack.isEmpty()).to.equal(true);
118+
119+
stack.push(1);
120+
stack.push(2);
121+
122+
stack.clear();
123+
expect(stack.isEmpty()).to.equal(true);
124+
});
125+
126+
it('returns an Array', () => {
127+
let stackArray = stack.toArray();
128+
expect(stackArray.length).to.equal(0);
129+
130+
stack.push(1);
131+
stack.push(2);
132+
133+
stackArray = stack.toArray();
134+
expect(stackArray.length).to.equal(2);
135+
136+
let i = 1;
137+
stackArray.forEach(e => {
138+
expect(e).to.equal(i);
139+
i++;
140+
});
141+
});
142+
143+
it('returns toString primitive types', () => {
144+
expect(stack.toString()).to.equal('');
145+
146+
stack.push(1);
147+
expect(stack.toString()).to.equal('1');
148+
149+
stack.push(2);
150+
expect(stack.toString()).to.equal('1,2');
151+
152+
stack.clear();
153+
expect(stack.toString()).to.equal('');
154+
155+
const stackString = new StackArray<string>();
156+
stackString.push('el1');
157+
expect(stackString.toString()).to.equal('el1');
158+
159+
stackString.push('el2');
160+
expect(stackString.toString()).to.equal('el1,el2');
161+
});
162+
163+
it('returns toString objects', () => {
164+
165+
class MyObj {
166+
constructor(public el1: any, public el2: any) { }
167+
toString() {
168+
return `${this.el1.toString()}|${this.el2.toString()}`;
169+
}
170+
}
171+
const stackMyObj = new StackArray<MyObj>();
172+
expect(stackMyObj.toString()).to.equal('');
173+
174+
stackMyObj.push(new MyObj(1, 2));
175+
expect(stackMyObj.toString()).to.equal('1|2');
176+
177+
stackMyObj.push(new MyObj(3, 4));
178+
expect(stackMyObj.toString()).to.equal('1|2,3|4');
179+
});
180+
});

‎test/ts/data-structures/stack.spec.ts

-17
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,6 @@ describe('Stack', () => {
123123
expect(stack.isEmpty()).to.equal(true);
124124
});
125125

126-
it('returns an Array', () => {
127-
let stackArray = stack.toArray();
128-
expect(stackArray.length).to.equal(0);
129-
130-
stack.push(1);
131-
stack.push(2);
132-
133-
stackArray = stack.toArray();
134-
expect(stackArray.length).to.equal(2);
135-
136-
let i = 1;
137-
stackArray.forEach(e => {
138-
expect(e).to.equal(i);
139-
i++;
140-
});
141-
});
142-
143126
it('returns toString primitive types', () => {
144127
expect(stack.toString()).to.equal('');
145128

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { parenthesesChecker } from '../../../src/ts/others/balanced-symbols';
4+
5+
describe('Balanced Symbols', () => {
6+
7+
it('empty to be falsy', () => {
8+
expect(parenthesesChecker('')).to.equal(true);
9+
});
10+
11+
it('{ to be falsy', () => {
12+
expect(parenthesesChecker('{')).to.equal(false);
13+
});
14+
15+
it('} to be falsy', () => {
16+
expect(parenthesesChecker('}')).to.equal(false);
17+
});
18+
19+
it('11 to be falsy', () => {
20+
expect(parenthesesChecker('11')).to.equal(false);
21+
});
22+
23+
it('{11 to be falsy', () => {
24+
expect(parenthesesChecker('{11')).to.equal(false);
25+
});
26+
27+
it('{([1])} to be falsy', () => {
28+
expect(parenthesesChecker('{([1])}')).to.equal(false);
29+
});
30+
31+
it('{([])} to be truthy', () => {
32+
expect(parenthesesChecker('{([])}')).to.equal(true);
33+
});
34+
35+
it('{{([][])}()} to be truthy', () => {
36+
expect(parenthesesChecker('{{([][])}()}')).to.equal(true);
37+
});
38+
39+
it('[{()] to be falsy', () => {
40+
expect(parenthesesChecker('[{()]')).to.equal(false);
41+
});
42+
});

‎test/ts/others/base-converter.spec.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { baseConverter, decimalToBinary } from '../../../src/ts/others/base-converter';
4+
5+
describe('Base Converter', () => {
6+
it('decimalToBinary 1 -> 1', () => {
7+
expect(decimalToBinary(1)).to.equal('1');
8+
});
9+
10+
it('decimalToBinary 2 -> 11', () => {
11+
expect(decimalToBinary(2)).to.equal('10');
12+
});
13+
14+
it('decimalToBinary 233 -> 11101001', () => {
15+
expect(decimalToBinary(233)).to.equal('11101001');
16+
});
17+
18+
it('decimalToBinary 10 -> 1010', () => {
19+
expect(decimalToBinary(10)).to.equal('1010');
20+
});
21+
22+
it('decimalToBinary 1000 -> 1111101000', () => {
23+
expect(decimalToBinary(1000)).to.equal('1111101000');
24+
});
25+
26+
it('baseConverter 100345, 2 -> 11000011111111001', () => {
27+
expect(baseConverter(100345, 2)).to.equal('11000011111111001');
28+
});
29+
30+
it('baseConverter 100345, 8 -> 303771', () => {
31+
expect(baseConverter(100345, 8)).to.equal('303771');
32+
});
33+
34+
it('baseConverter 100345, 16 -> 187F9', () => {
35+
expect(baseConverter(100345, 16)).to.equal('187F9');
36+
});
37+
38+
it('baseConverter 100345, 7 -> 565360', () => {
39+
expect(baseConverter(100345, 7)).to.equal('565360');
40+
});
41+
42+
it('baseConverter 100345, 20 -> CAH5', () => {
43+
expect(baseConverter(100345, 20)).to.equal('CAH5');
44+
});
45+
46+
it('baseConverter 100345, 35 -> 2BW0', () => {
47+
expect(baseConverter(100345, 35)).to.equal('2BW0');
48+
});
49+
50+
it('baseConverter 100345, 36 -> 25FD', () => {
51+
expect(baseConverter(100345, 36)).to.equal('25FD');
52+
});
53+
54+
it('baseConverter 100345, 37 -> ', () => {
55+
expect(baseConverter(100345, 37)).to.equal('');
56+
});
57+
});

‎test/ts/others/hanoi.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { hanoi, hanoiStack } from '../../../src/ts/others/hanoi';
4+
5+
describe('Tower of Hanoi', () => {
6+
7+
it('Hanoi', () => {
8+
for (let i = 0; i < 10; i++) {
9+
const result = hanoi(i, 'a', 'b', 'c');
10+
expect(result.length).to.equal(2 ** i - 1);
11+
}
12+
});
13+
14+
it('Hanoi with Stack', () => {
15+
for (let i = 0; i < 10; i++) {
16+
const result = hanoiStack(i);
17+
expect(result.length).to.equal(2 ** i - 1);
18+
}
19+
});
20+
});

‎webpack.config.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// @ts-check
2+
/* eslint-disable */
3+
const webpack = require('webpack');
4+
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
5+
const path = require('path');
6+
const env = require('yargs').argv.env;
7+
8+
let libraryName = 'PacktDataStructuresAlgorithms';
9+
10+
let plugins = [],
11+
outputFile;
12+
13+
if (env === 'build') {
14+
plugins.push(new UglifyJsPlugin({ minimize: true }));
15+
outputFile = libraryName + '.min.js';
16+
} else {
17+
outputFile = libraryName + '.js';
18+
}
19+
20+
const config = {
21+
entry: __dirname + '/src/js/index.js',
22+
devtool: 'source-map',
23+
output: {
24+
path: __dirname + '/examples',
25+
filename: outputFile,
26+
library: libraryName,
27+
libraryTarget: 'umd',
28+
umdNamedDefine: true
29+
},
30+
module: {
31+
rules: [
32+
{
33+
test: /(\.jsx|\.js)$/,
34+
loader: 'babel-loader',
35+
exclude: /(node_modules|bower_components)/
36+
}
37+
]
38+
},
39+
resolve: {
40+
modules: [path.resolve('./node_modules'), path.resolve('./src/js')],
41+
extensions: ['.json', '.js']
42+
},
43+
plugins: plugins
44+
};
45+
46+
module.exports = config;

0 commit comments

Comments
 (0)
Please sign in to comment.