Skip to content

Commit 054cbff

Browse files
authored
Merge pull request loiane#76 from loiane/third-edition
Third edition
2 parents 13ad569 + 15f2925 commit 054cbff

10 files changed

+121
-6
lines changed

src/ts/algorithms/greedy/knapsack.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function knapSack(capacity: number, weights: number[], values: number[]) {
1+
export function knapSack(capacity: number, weights: number[], values: number[]) {
22
const n = values.length;
33
let load = 0;
44
let val = 0;

src/ts/algorithms/greedy/longest-common-subsequence.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function lcs(wordX: string, wordY: string, m = wordX.length, n = wordY.length): number {
1+
export function lcs(wordX: string, wordY: string, m = wordX.length, n = wordY.length): number {
22
if (m === 0 || n === 0) {
33
return 0;
44
}

src/ts/algorithms/greedy/min-coin-change.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function minCoinChange(coins: number[], amount: number) {
1+
export function minCoinChange(coins: number[], amount: number) {
22
const change: number[] = [];
33
let total = 0;
44
for (let i = coins.length; i >= 0; i--) {

src/ts/index.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,17 @@ export { findMinValue as findMinValue } from './algorithms/search/min-max-search
6363

6464
// chapter 14
6565
export { binarySearch as binarySearchRecursive } from './algorithms/search/binary-search-recursive';
66-
export { minCoinChange as minCoinChange } from './algorithms/dynamic-programing/min-coin-change';
67-
export { ratInAMaze as ratInAMaze } from './algorithms/backtracking/rat-in-maze';
68-
export { sudokuSolver as sudokuSolver } from './algorithms/backtracking/sudoku-solver';
66+
export { minCoinChange } from './algorithms/dynamic-programing/min-coin-change';
67+
export { minCoinChange as minCoinChangeGreedy } from './algorithms/greedy/min-coin-change';
68+
export { knapSack } from './algorithms/dynamic-programing/knapsack';
69+
export { knapSack as knapSackRecursive } from './algorithms/dynamic-programing/knapsack-recursive';
70+
export { knapSack as knapSackGreedy } from './algorithms/greedy/knapsack';
71+
export { lcs } from './algorithms/dynamic-programing/longest-common-subsequence';
72+
export { lcs as lcsPrint } from './algorithms/dynamic-programing/longest-common-subsequence-print';
73+
export { lcs as lcsRecursive } from './algorithms/greedy/longest-common-subsequence';
74+
export { ratInAMaze } from './algorithms/backtracking/rat-in-maze';
75+
export { sudokuSolver } from './algorithms/backtracking/sudoku-solver';
76+
6977

7078

7179
/* import { hotPotato } from './others/hot-potato';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { ratInAMaze } from '../../../../src/js/index';
4+
5+
describe('Rat in a maze', () => {
6+
it('rat in a maze solver', () => {
7+
const maze = [
8+
[1, 0, 0, 0],
9+
[1, 1, 1, 1],
10+
[0, 0, 1, 0],
11+
[0, 1, 1, 1]
12+
];
13+
const solution = [
14+
[1, 0, 0, 0],
15+
[1, 1, 1, 0],
16+
[0, 0, 1, 0],
17+
[0, 0, 1, 1]
18+
];
19+
expect(ratInAMaze(maze)).to.deep.equal(solution);
20+
});
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { sudokuSolver } from '../../../../src/js/index';
4+
5+
describe('Sudoku Solver', () => {
6+
it('sudoku solver', () => {
7+
const grid = [
8+
[3, 0, 6, 5, 0, 8, 4, 0, 0],
9+
[5, 2, 0, 0, 0, 0, 0, 0, 0],
10+
[0, 8, 7, 0, 0, 0, 0, 3, 1],
11+
[0, 0, 3, 0, 1, 0, 0, 8, 0],
12+
[9, 0, 0, 8, 6, 3, 0, 0, 5],
13+
[0, 5, 0, 0, 9, 0, 6, 0, 0],
14+
[1, 3, 0, 0, 0, 0, 2, 5, 0],
15+
[0, 0, 0, 0, 0, 0, 0, 7, 4],
16+
[0, 0, 5, 2, 0, 6, 3, 0, 0]
17+
];
18+
const solution = [
19+
[3, 1, 6, 5, 7, 8, 4, 9, 2],
20+
[5, 2, 9, 1, 3, 4, 7, 6, 8],
21+
[4, 8, 7, 6, 2, 9, 5, 3, 1],
22+
[2, 6, 3, 4, 1, 5, 9, 8, 7],
23+
[9, 7, 4, 8, 6, 3, 1, 2, 5],
24+
[8, 5, 1, 7, 9, 2, 6, 4, 3],
25+
[1, 3, 8, 9, 4, 7, 2, 5, 6],
26+
[6, 9, 2, 3, 5, 1, 8, 7, 4],
27+
[7, 4, 5, 2, 8, 6, 3, 1, 9]
28+
];
29+
expect(sudokuSolver(grid)).to.deep.equal(solution);
30+
});
31+
32+
it('sudoku solver 2', () => {
33+
const grid = [
34+
[5, 3, 0, 0, 7, 0, 0, 0, 0],
35+
[6, 0, 0, 1, 9, 5, 0, 0, 0],
36+
[0, 9, 8, 0, 0, 0, 0, 6, 0],
37+
[8, 0, 0, 0, 6, 0, 0, 0, 3],
38+
[4, 0, 0, 8, 0, 3, 0, 0, 1],
39+
[7, 0, 0, 0, 2, 0, 0, 0, 6],
40+
[0, 6, 0, 0, 0, 0, 2, 8, 0],
41+
[0, 0, 0, 4, 1, 9, 0, 0, 5],
42+
[0, 0, 0, 0, 8, 0, 0, 7, 9]
43+
];
44+
const solution = [
45+
[5, 3, 4, 6, 7, 8, 9, 1, 2],
46+
[6, 7, 2, 1, 9, 5, 3, 4, 8],
47+
[1, 9, 8, 3, 4, 2, 5, 6, 7],
48+
[8, 5, 9, 7, 6, 1, 4, 2, 3],
49+
[4, 2, 6, 8, 5, 3, 7, 9, 1],
50+
[7, 1, 3, 9, 2, 4, 8, 5, 6],
51+
[9, 6, 1, 5, 3, 7, 2, 8, 4],
52+
[2, 8, 7, 4, 1, 9, 6, 3, 5],
53+
[3, 4, 5, 2, 8, 6, 1, 7, 9]
54+
];
55+
expect(sudokuSolver(grid)).to.deep.equal(solution);
56+
});
57+
});
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { knapSackGreedy } from '../../../../src/ts/index';
4+
5+
describe('Min Coin Change Greedy', () => {
6+
const SIZE = 100;
7+
8+
it('works with greedy approach', () => {
9+
const values = [3, 4, 5];
10+
const weights = [2, 3, 4];
11+
const capacity = 5;
12+
13+
expect(knapSackGreedy(capacity, weights, values)).to.equal(7);
14+
});
15+
});

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

Whitespace-only changes.

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import { minCoinChangeGreedy } from '../../../../src/ts/index';
4+
5+
describe('Min Coin Change Greedy', () => {
6+
7+
const SIZE = 100;
8+
9+
it('works with greedy approach', () => {
10+
expect(minCoinChangeGreedy([1, 5, 10], 15)).to.deep.equal([10, 5]);
11+
expect(minCoinChangeGreedy([1, 3, 4], 6)).to.deep.equal([4, 1, 1]);
12+
});
13+
14+
});

0 commit comments

Comments
 (0)