Skip to content

Commit 873f929

Browse files
committed
[Algorithm Design and Techniques]
1 parent 302667d commit 873f929

22 files changed

+216
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { matrixChainOrder } = PacktDataStructuresAlgorithms;
22

33
const p = [10, 100, 5, 50, 1];
4-
const n = p.length;
5-
console.log(matrixChainOrder(p, n));
4+
console.log(matrixChainOrder(p));
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { matrixChainOrderGreedy } = PacktDataStructuresAlgorithms;
22

33
const p = [10, 100, 5, 50, 1];
4-
const n = p.length;
5-
console.log(matrixChainOrderGreedy(p, n));
4+
console.log(matrixChainOrderGreedy(p));

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+
});

test/ts/algorithms/dynamic-programming/knapsack-recursive.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { expect } from 'chai';
33
import { knapSackRecursive } from '../../../../src/ts/index';
44

55
describe('KnapSack Dynamic Programming - Recursive', () => {
6-
const SIZE = 100;
76

87
it('works with recursive approach', () => {
98
const values = [3, 4, 5];

test/ts/algorithms/dynamic-programming/knapsack.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { expect } from 'chai';
33
import { knapSack } from '../../../../src/ts/index';
44

55
describe('KnapSack Dynamic Programming', () => {
6-
const SIZE = 100;
76

87
it('works with DP 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 { 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
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { expect } from 'chai';
33
import { knapSackGreedy } from '../../../../src/ts/index';
44

55
describe('KnapSack Greedy', () => {
6-
const SIZE = 100;
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)