Skip to content

Commit 7df2a07

Browse files
committedMay 1, 2018
[Algorithm Design and Techniques]
1 parent 15f2925 commit 7df2a07

16 files changed

+2671
-2466
lines changed
 

‎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,5 @@
1+
const { matrixChainOrder } = PacktDataStructuresAlgorithms;
2+
3+
const p = [10, 100, 5, 50, 1];
4+
const n = p.length;
5+
console.log(matrixChainOrder(p, n));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { matrixChainOrderGreedy } = PacktDataStructuresAlgorithms;
2+
3+
const p = [10, 100, 5, 50, 1];
4+
const n = p.length;
5+
console.log(matrixChainOrderGreedy(p, n));
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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { knapSackRecursive } from '../../../../src/ts/index';
4+
5+
describe('KnapSack Dynamic Programming - Recursive', () => {
6+
const SIZE = 100;
7+
8+
it('works with recursive approach', () => {
9+
const values = [3, 4, 5];
10+
const weights = [2, 3, 4];
11+
const capacity = 5;
12+
const n = values.length;
13+
14+
expect(knapSackRecursive(capacity, weights, values, n)).to.equal(7);
15+
});
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { knapSack } from '../../../../src/ts/index';
4+
5+
describe('KnapSack Dynamic Programming', () => {
6+
const SIZE = 100;
7+
8+
it('works with DP approach', () => {
9+
const values = [3, 4, 5];
10+
const weights = [2, 3, 4];
11+
const capacity = 5;
12+
const n = values.length;
13+
14+
expect(knapSack(capacity, weights, values, n)).to.equal(7);
15+
});
16+
});

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

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

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

88
it('works with greedy approach', () => {

‎test/ts/algorithms/greedy/longest-common-subsequence.spec.ts

Whitespace-only changes.

‎test/ts/algorithms/greedy/matrix-chain-multiplication.spec.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)
Please sign in to comment.