Skip to content

Commit 25a4dff

Browse files
authoredMay 1, 2018
Merge pull request loiane#80 from loiane/third-edition
[Algorithm Design and Techniques]
2 parents 03f832a + 873f929 commit 25a4dff

32 files changed

+2883
-2470
lines changed
 

‎.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"no-return-assign": 0,
3030
"no-restricted-globals": 0,
3131
"no-multi-assign": 0,
32-
"prefer-destructuring": ["error", {"object": true, "array": false}]
32+
"prefer-destructuring": ["error", {"object": true, "array": false}],
33+
"padded-blocks": 0
3334
}
3435
}

‎examples/PacktDataStructuresAlgorithms.min.js

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

‎examples/PacktDataStructuresAlgorithms.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { lcs } = PacktDataStructuresAlgorithms;
2+
const { lcsPrint } = PacktDataStructuresAlgorithms;
3+
4+
const wordX = 'acbaed';
5+
const wordY = 'abcadf';
6+
7+
console.log('lcs', lcs(wordX, wordY));
8+
console.log('lcsPrint', lcsPrint(wordX, wordY));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { lcsRecursive } = PacktDataStructuresAlgorithms;
2+
3+
const wordX = 'acbaed';
4+
const wordY = 'abcadf';
5+
6+
console.log('lcsRecursive', lcsRecursive(wordX, wordY));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { matrixChainOrder } = PacktDataStructuresAlgorithms;
2+
3+
const p = [10, 100, 5, 50, 1];
4+
console.log(matrixChainOrder(p));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { matrixChainOrderGreedy } = PacktDataStructuresAlgorithms;
2+
3+
const p = [10, 100, 5, 50, 1];
4+
console.log(matrixChainOrderGreedy(p));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
console.log('Using imperative JS');
2+
3+
var printArray = function(array){
4+
for (var i=0; i<array.length; i++){
5+
console.log(array[i]);
6+
}
7+
};
8+
9+
printArray([1, 2, 3, 4, 5]);
10+
11+
//how can we abstract the For flow? Can we use a callback for action?
12+
13+
console.log('Using functional JS');
14+
15+
var forEach = function(array, action){
16+
for (var i=0; i<array.length; i++){
17+
action(array[i]);
18+
}
19+
};
20+
21+
var logItem = function (item) {
22+
console.log(item);
23+
};
24+
25+
forEach([1, 2, 3, 4, 5], logItem);
26+
27+
//how can we abstract the For flow?
28+
console.log('Finding the min value in an array - imperative');
29+
30+
var findMinArray = function(array){
31+
var minValue = array[0];
32+
for (var i=1; i<array.length; i++){
33+
if (minValue > array[i]){
34+
minValue = array[i];
35+
}
36+
}
37+
38+
return minValue;
39+
};
40+
41+
console.log(findMinArray([8,6,4,5,9]));
42+
43+
console.log('Finding the min value in an array - functional ES2015');
44+
const min_ = function(array){
45+
return Math.min(...array)
46+
};
47+
48+
//simplifying using arrow functions
49+
const min = arr => Math.min(...arr);
50+
51+
console.log(min_([8,6,4,5,9]));
52+
console.log(min([8,6,4,5,9]));
53+
54+
//concat + reduce
55+
console.log('merge arrays - imperative');
56+
57+
var mergeArrays_ = function(arrays){
58+
var count = arrays.length,
59+
newArray = [],
60+
k =0;
61+
for (var i=0; i<count; i++){
62+
for (var j=0; j<arrays[i].length; j++){
63+
newArray[k++] = arrays[i][j];
64+
}
65+
}
66+
return newArray;
67+
};
68+
69+
console.log(mergeArrays_([[1, 2, 3], [4, 5], [6]]));
70+
71+
console.log('merge arrays - using concat');
72+
var mergeArraysConcat = function(arrays){
73+
return arrays.reduce( function(p,n){
74+
return p.concat(n);
75+
});
76+
};
77+
78+
console.log(mergeArraysConcat([[1, 2, 3], [4, 5], [6]]));
79+
80+
console.log('merge arrays - ES2015');
81+
82+
const mergeArrays = (...arrays) => [].concat(...arrays);
83+
console.log(mergeArrays([1, 2, 3], [4, 5], [6]));
84+
85+
console.log('sum values of arrays - imperative');
86+
var sumValues = function(array){
87+
var total = array[0];
88+
for (var i=1; i<array.length; i++){
89+
total += array[i];
90+
}
91+
return total;
92+
};
93+
94+
console.log(sumValues([1, 2, 3, 4, 5]));
95+
96+
//reduce
97+
console.log('sum values of arrays - functional');
98+
var sum_ = function(array){
99+
return array.reduce(function(a, b){
100+
return a + b;
101+
})
102+
};
103+
104+
console.log(sum_([1, 2, 3, 4, 5]));
105+
106+
console.log('sum values of arrays - ES2015');
107+
const sum = arr => arr.reduce((a, b) => a + b);
108+
109+
console.log(sum([1, 2, 3, 4, 5]));
110+
111+
//map
112+
var daysOfWeek = [
113+
{name: 'Monday', value: 1},
114+
{name: 'Tuesday', value: 2},
115+
{name: 'Wednesday', value: 7}
116+
];
117+
118+
var daysOfWeekValues_ = [];
119+
for (var i = 0; i < daysOfWeek.length; i++) {
120+
daysOfWeekValues_.push(daysOfWeek[i].value);
121+
}
122+
123+
//to
124+
var daysOfWeekValues = daysOfWeek.map(function(day) {
125+
return day.value;
126+
});
127+
console.log(daysOfWeekValues);
128+
129+
130+
//filter
131+
var positiveNumbers_ = function(array){
132+
var positive = [];
133+
for (var i = 0; i < array.length; i++) {
134+
if (array[i] >= 0){
135+
positive.push(array[i]);
136+
}
137+
}
138+
return positive;
139+
}
140+
console.log(positiveNumbers_([-1,1,2,-2]));
141+
142+
var positiveNumbers = function(array){
143+
return array.filter(function(num){
144+
return num >= 0;
145+
})
146+
};
147+
console.log(positiveNumbers([-1,1,2,-2]));

‎package-lock.json

+2,458-2,458
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/js/algorithms/dynamic-programing/longest-common-subsequence-print.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function printSolution(solution, wordX, m, n) {
1515
}
1616
x = solution[a][b];
1717
}
18-
// console.log('lcs: ' + answer);
18+
return answer;
1919
}
2020
export function lcs(wordX, wordY) {
2121
const m = wordX.length;
@@ -47,6 +47,5 @@ export function lcs(wordX, wordY) {
4747
// console.log(l[i].join());
4848
// console.log(solution[i].join());
4949
}
50-
printSolution(solution, wordX, m, n);
51-
return l[m][n];
50+
return printSolution(solution, wordX, m, n);
5251
}

‎src/js/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,7 @@ export { knapSack as knapSackGreedy } from './algorithms/greedy/knapsack';
9292
export { lcs } from './algorithms/dynamic-programing/longest-common-subsequence';
9393
export { lcs as lcsPrint } from './algorithms/dynamic-programing/longest-common-subsequence-print';
9494
export { lcs as lcsRecursive } from './algorithms/greedy/longest-common-subsequence';
95+
export { matrixChainOrder } from './algorithms/dynamic-programing/matrix-chain-multiplication';
96+
export { matrixChainOrder as matrixChainOrderGreedy } from './algorithms/greedy/matrix-chain-multiplication';
9597
export { ratInAMaze } from './algorithms/backtracking/rat-in-maze';
9698
export { sudokuSolver } from './algorithms/backtracking/sudoku-solver';

‎src/ts/algorithms/dynamic-programing/longest-common-subsequence-print.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ export function lcs(wordX: string, wordY: string) {
3131
// console.log(solution[i].join());
3232
}
3333

34-
printSolution(solution, wordX, m, n);
34+
return printSolution(solution, wordX, m, n);
3535

36-
return l[m][n];
36+
// return l[m][n];
3737
}
3838

3939
function printSolution(solution: Array<Array<string>>, wordX: string, m: number, n: number) {
@@ -55,5 +55,6 @@ function printSolution(solution: Array<Array<string>>, wordX: string, m: number,
5555
x = solution[a][b];
5656
}
5757

58+
return answer;
5859
// console.log('lcs: ' + answer);
5960
}

‎src/ts/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export { knapSack as knapSackGreedy } from './algorithms/greedy/knapsack';
7171
export { lcs } from './algorithms/dynamic-programing/longest-common-subsequence';
7272
export { lcs as lcsPrint } from './algorithms/dynamic-programing/longest-common-subsequence-print';
7373
export { lcs as lcsRecursive } from './algorithms/greedy/longest-common-subsequence';
74+
export { matrixChainOrder } from './algorithms/dynamic-programing/matrix-chain-multiplication';
75+
export { matrixChainOrder as matrixChainOrderGreedy } from './algorithms/greedy/matrix-chain-multiplication';
7476
export { ratInAMaze } from './algorithms/backtracking/rat-in-maze';
7577
export { sudokuSolver } from './algorithms/backtracking/sudoku-solver';
7678

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { knapSackRecursive } from '../../../../src/js/index';
4+
5+
describe('KnapSack Dynamic Programming - Recursive', () => {
6+
7+
it('works with recursive approach', () => {
8+
const values = [3, 4, 5];
9+
const weights = [2, 3, 4];
10+
const capacity = 5;
11+
const n = values.length;
12+
13+
expect(knapSackRecursive(capacity, weights, values, n)).to.equal(7);
14+
});
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { knapSack } from '../../../../src/js/index';
4+
5+
describe('KnapSack Dynamic Programming', () => {
6+
7+
it('works with DP approach', () => {
8+
const values = [3, 4, 5];
9+
const weights = [2, 3, 4];
10+
const capacity = 5;
11+
const n = values.length;
12+
13+
expect(knapSack(capacity, weights, values, n)).to.equal(7);
14+
});
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { lcsPrint } from '../../../../src/js/index';
4+
5+
describe('LCS Dynamic Programming with print solution', () => {
6+
7+
it('works with DP approach with print solution', () => {
8+
const wordX = 'acbaed';
9+
const wordY = 'abcadf';
10+
11+
expect(lcsPrint(wordX, wordY)).to.equal('acad');
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { lcs } from '../../../../src/js/index';
4+
5+
describe('LCS Dynamic Programming', () => {
6+
7+
it('works with DP approach', () => {
8+
const wordX = 'acbaed';
9+
const wordY = 'abcadf';
10+
11+
expect(lcs(wordX, wordY)).to.equal(4);
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { matrixChainOrder } from '../../../../src/js/index';
4+
5+
describe('Matrix Chain Multiplication', () => {
6+
7+
it('works with DP approach', () => {
8+
const p = [10, 100, 5, 50, 1];
9+
10+
expect(matrixChainOrder(p)).to.equal(1750);
11+
});
12+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { minCoinChange } from '../../../../src/js/index';
4+
5+
describe('Dynamic Programming: Min Coin Change', () => {
6+
7+
it('works with amount 0', () => {
8+
expect(minCoinChange([1, 2, 3], 0)).to.deep.equal([]);
9+
});
10+
11+
it('works with amount 1', () => {
12+
expect(minCoinChange([1, 2, 3], 1)).to.deep.equal([1]);
13+
});
14+
15+
it('works with amount 2', () => {
16+
expect(minCoinChange([1, 2, 3], 2)).to.deep.equal([2]);
17+
});
18+
19+
it('works with amount 3', () => {
20+
expect(minCoinChange([1, 2, 3], 3)).to.deep.equal([3]);
21+
});
22+
23+
it('works with amount 4', () => {
24+
expect(minCoinChange([1, 2, 3], 4)).to.deep.equal([1, 3]);
25+
});
26+
27+
it('works with amount 6', () => {
28+
expect(minCoinChange([1, 2, 3], 6)).to.deep.equal([3, 3]);
29+
});
30+
});
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { knapSackGreedy } from '../../../../src/js/index';
4+
5+
describe('KnapSack Greedy', () => {
6+
7+
it('works with greedy approach', () => {
8+
const values = [3, 4, 5];
9+
const weights = [2, 3, 4];
10+
const capacity = 5;
11+
12+
expect(knapSackGreedy(capacity, weights, values)).to.equal(7);
13+
});
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { lcsRecursive } from '../../../../src/js/index';
4+
5+
describe('LCS Greedy', () => {
6+
7+
it('works with Greedy approach', () => {
8+
const wordX = 'acbaed';
9+
const wordY = 'abcadf';
10+
11+
expect(lcsRecursive(wordX, wordY)).to.equal(4);
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { matrixChainOrderGreedy } from '../../../../src/js/index';
4+
5+
describe('Matrix Chain Multiplication', () => {
6+
7+
it('works with DP approach', () => {
8+
const p = [10, 100, 5, 50, 1];
9+
10+
expect(matrixChainOrderGreedy(p)).to.equal(1750);
11+
});
12+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { minCoinChangeGreedy } from '../../../../src/js/index';
4+
5+
describe('Min Coin Change Greedy', () => {
6+
7+
it('works with greedy approach', () => {
8+
expect(minCoinChangeGreedy([1, 5, 10], 15)).to.deep.equal([10, 5]);
9+
expect(minCoinChangeGreedy([1, 3, 4], 6)).to.deep.equal([4, 1, 1]);
10+
});
11+
12+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { knapSackRecursive } from '../../../../src/ts/index';
4+
5+
describe('KnapSack Dynamic Programming - Recursive', () => {
6+
7+
it('works with recursive approach', () => {
8+
const values = [3, 4, 5];
9+
const weights = [2, 3, 4];
10+
const capacity = 5;
11+
const n = values.length;
12+
13+
expect(knapSackRecursive(capacity, weights, values, n)).to.equal(7);
14+
});
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { knapSack } from '../../../../src/ts/index';
4+
5+
describe('KnapSack Dynamic Programming', () => {
6+
7+
it('works with DP approach', () => {
8+
const values = [3, 4, 5];
9+
const weights = [2, 3, 4];
10+
const capacity = 5;
11+
const n = values.length;
12+
13+
expect(knapSack(capacity, weights, values, n)).to.equal(7);
14+
});
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { lcsPrint } from '../../../../src/ts/index';
4+
5+
describe('LCS Dynamic Programming with print solution', () => {
6+
7+
it('works with DP approach with print solution', () => {
8+
const wordX = 'acbaed';
9+
const wordY = 'abcadf';
10+
11+
expect(lcsPrint(wordX, wordY)).to.equal('acad');
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { lcs } from '../../../../src/ts/index';
4+
5+
describe('LCS Dynamic Programming', () => {
6+
7+
it('works with DP approach', () => {
8+
const wordX = 'acbaed';
9+
const wordY = 'abcadf';
10+
11+
expect(lcs(wordX, wordY)).to.equal(4);
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { matrixChainOrder } from '../../../../src/ts/index';
4+
5+
describe('Matrix Chain Multiplication', () => {
6+
7+
it('works with DP approach', () => {
8+
const p = [10, 100, 5, 50, 1];
9+
10+
expect(matrixChainOrder(p)).to.equal(1750);
11+
});
12+
});

‎test/ts/algorithms/greedy/knapsack.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import 'mocha';
22
import { expect } from 'chai';
33
import { knapSackGreedy } from '../../../../src/ts/index';
44

5-
describe('Min Coin Change Greedy', () => {
6-
const SIZE = 100;
5+
describe('KnapSack Greedy', () => {
76

87
it('works with greedy approach', () => {
98
const values = [3, 4, 5];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { lcsRecursive } from '../../../../src/ts/index';
4+
5+
describe('LCS Greedy', () => {
6+
7+
it('works with Greedy approach', () => {
8+
const wordX = 'acbaed';
9+
const wordY = 'abcadf';
10+
11+
expect(lcsRecursive(wordX, wordY)).to.equal(4);
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { matrixChainOrderGreedy } from '../../../../src/ts/index';
4+
5+
describe('Matrix Chain Multiplication', () => {
6+
7+
it('works with DP approach', () => {
8+
const p = [10, 100, 5, 50, 1];
9+
10+
expect(matrixChainOrderGreedy(p)).to.equal(1750);
11+
});
12+
});

‎test/ts/algorithms/greedy/min-coin-change.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { minCoinChangeGreedy } from '../../../../src/ts/index';
44

55
describe('Min Coin Change Greedy', () => {
66

7-
const SIZE = 100;
8-
97
it('works with greedy approach', () => {
108
expect(minCoinChangeGreedy([1, 5, 10], 15)).to.deep.equal([10, 5]);
119
expect(minCoinChangeGreedy([1, 3, 4], 6)).to.deep.equal([4, 1, 1]);

0 commit comments

Comments
 (0)
Please sign in to comment.