Skip to content

Commit 6e9eb98

Browse files
authored
Merge pull request loiane#54 from loiane/third-edition
Third edition
2 parents 0a68c19 + 1ff78ec commit 6e9eb98

35 files changed

+293
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ coverage
66
coverage.lcov
77
mochawesome-report/*
88
dist/js/*
9+
dist-/*
910
dist/ts/*
1011
snippet.js

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Learning JavaScript Data Structures and Algorithms
22
====================================
33

44
[![Build Status](https://travis-ci.org/loiane/javascript-datastructures-algorithms.svg?branch=third-edition)](https://travis-ci.org/loiane/javascript-datastructures-algorithms)
5-
[![codecov](https://codecov.io/gh/loiane/javascript-datastructures-algorithms/branch/third-edition/graph/badge.svg)](https://codecov.io/gh/loiane/javascript-datastructures-algorithms)
5+
[![codecov](https://codecov.io/gh/loiane/javascript-datastructures-algorithms/branch/master/graph/badge.svg)](https://codecov.io/gh/loiane/javascript-datastructures-algorithms)
66
[![devDependencies Status](https://david-dm.org/loiane/javascript-datastructures-algorithms/dev-status.svg)](https://david-dm.org/loiane/javascript-datastructures-algorithms?type=dev)
77
[![dependencies Status](https://david-dm.org/loiane/javascript-datastructures-algorithms/status.svg)](https://david-dm.org/loiane/javascript-datastructures-algorithms)
88
[![Greenkeeper badge](https://badges.greenkeeper.io/loiane/javascript-datastructures-algorithms.svg)](https://greenkeeper.io/)

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.
+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="./01-DC-BinarySearch.js"></script>
10+
</body>
11+
</html>

examples/chapter14/01-DC-BinarySearch.js

Whitespace-only changes.
+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="./02-MinCoinChangeDP.js"></script>
10+
</body>
11+
</html>
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { minCoinChange } = PacktDataStructuresAlgorithms;
2+
3+
console.log(minCoinChange([1, 5, 10], 15)); // [5, 10]
4+
console.log(minCoinChange([1, 3, 4], 6)); // [3, 3]
5+
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-MinCoinChangeGreedy.js"></script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { minCoinChangeGreedy } = PacktDataStructuresAlgorithms;
2+
3+
console.log(minCoinChangeGreedy([1, 5, 10], 15)); // [5, 10]
4+
console.log(minCoinChangeGreedy([1, 3, 4], 6)); // [4, 1, 1]
5+
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-KnapsackProblemDP.js"></script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { knapSack } = PacktDataStructuresAlgorithms;
2+
3+
const values = [3,4,5];
4+
const weights = [2,3,4];
5+
const capacity = 5;
6+
const n = values.length;
7+
8+
console.log(knapSack(capacity, weights, values, n));
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="./05-KnapSackProblemRecursive.js"></script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { knapSackRecursive } = PacktDataStructuresAlgorithms;
2+
3+
const values = [3,4,5];
4+
const weights = [2,3,4];
5+
const capacity = 5;
6+
const n = values.length;
7+
8+
console.log(knapSackRecursive(capacity, weights, values, n));
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="./06-KnapSackProblemGreedy.js"></script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { knapSackGreedy } = PacktDataStructuresAlgorithms;
2+
3+
const values = [3,4,5];
4+
const weights = [2,3,4];
5+
const capacity = 5;
6+
7+
console.log(knapSackGreedy(capacity, weights, values));
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="./07-LongestCommonSubsequenceDP.js"></script>
10+
</body>
11+
</html>

examples/chapter14/07-LongestCommonSubsequenceDP.js

Whitespace-only changes.
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="./08-LongestCommonSubsequenceRecursive.js"></script>
10+
</body>
11+
</html>

examples/chapter14/08-LongestCommonSubsequenceRecursive.js

Whitespace-only changes.
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="./09-MatrixChainMultiplicationDP.js"></script>
10+
</body>
11+
</html>

examples/chapter14/09-MatrixChainMultiplicationDP.js

Whitespace-only changes.
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="./10-MatrixChainMultiplicationRecursive.js"></script>
10+
</body>
11+
</html>

examples/chapter14/10-MatrixChainMultiplicationRecursive.js

Whitespace-only changes.

examples/chapter14/11-RatInMaze.html

+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="./11-RatInMaze.js"></script>
10+
</body>
11+
</html>

examples/chapter14/11-RatInMaze.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { ratInAMaze } = PacktDataStructuresAlgorithms;
2+
3+
const maze = [
4+
[1, 0, 0, 0],
5+
[1, 1, 1, 1],
6+
[0, 0, 1, 0],
7+
[0, 1, 1, 1]
8+
];
9+
10+
console.log(ratInAMaze(maze));
+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="./12-SudokuSolver.js"></script>
10+
</body>
11+
</html>

examples/chapter14/12-SudokuSolver.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { sudokuSolver } = PacktDataStructuresAlgorithms;
2+
3+
const sudokuGrid = [
4+
[5, 3, 0, 0, 7, 0, 0, 0, 0],
5+
[6, 0, 0, 1, 9, 5, 0, 0, 0],
6+
[0, 9, 8, 0, 0, 0, 0, 6, 0],
7+
[8, 0, 0, 0, 6, 0, 0, 0, 3],
8+
[4, 0, 0, 8, 0, 3, 0, 0, 1],
9+
[7, 0, 0, 0, 2, 0, 0, 0, 6],
10+
[0, 6, 0, 0, 0, 0, 2, 8, 0],
11+
[0, 0, 0, 4, 1, 9, 0, 0, 5],
12+
[0, 0, 0, 0, 8, 0, 0, 7, 9]
13+
];
14+
15+
console.log(sudokuSolver(sudokuGrid));
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="./13-IntroFunctionalProgramming.js"></script>
10+
</body>
11+
</html>

examples/chapter14/13-IntroFunctionalProgramming.js

Whitespace-only changes.

examples/index.html

+47
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
<a href="#scroll-tab-9" class="mdl-layout__tab">09</a>
5050
<a href="#scroll-tab-10" class="mdl-layout__tab">10</a>
5151
<a href="#scroll-tab-11" class="mdl-layout__tab">11</a>
52+
<a href="#scroll-tab-12" class="mdl-layout__tab">12</a>
53+
<a href="#scroll-tab-13" class="mdl-layout__tab">13</a>
54+
<a href="#scroll-tab-14" class="mdl-layout__tab">14</a>
55+
<a href="#scroll-tab-15" class="mdl-layout__tab">15</a>
5256
</div>
5357
</header>
5458
<main class="mdl-layout__content">
@@ -218,6 +222,49 @@
218222
</div>
219223
</div>
220224
</section>
225+
<section class="mdl-layout__tab-panel" id="scroll-tab-12">
226+
<div class="page-content">
227+
<div class="page-content mdl-layout--fixed-drawer">
228+
<div class="mdl-layout__drawer is-visible">
229+
<nav class="mdl-navigation">
230+
</nav>
231+
</div>
232+
</div>
233+
</div>
234+
</section>
235+
<section class="mdl-layout__tab-panel" id="scroll-tab-13">
236+
<div class="page-content">
237+
<div class="page-content mdl-layout--fixed-drawer">
238+
<div class="mdl-layout__drawer is-visible">
239+
<nav class="mdl-navigation">
240+
</nav>
241+
</div>
242+
</div>
243+
</div>
244+
</section>
245+
<section class="mdl-layout__tab-panel" id="scroll-tab-14">
246+
<div class="page-content">
247+
<div class="page-content mdl-layout--fixed-drawer">
248+
<div class="mdl-layout__drawer is-visible">
249+
<nav class="mdl-navigation">
250+
<a class="mdl-navigation__link" href="chapter14/01-DC-BinarySearch.html">01-DC-BinarySearch</a>
251+
<a class="mdl-navigation__link" href="chapter14/02-MinCoinChangeDP.html">02-MinCoinChangeDP</a>
252+
<a class="mdl-navigation__link" href="chapter14/03-MinCoinChangeGreedy.html">03-MinCoinChangeGreedy</a>
253+
<a class="mdl-navigation__link" href="chapter14/04-KnapsackProblemDP.html">04-KnapsackProblemDP</a>
254+
<a class="mdl-navigation__link" href="chapter14/05-KnapSackProblemRecursive.html">05-KnapSackProblemRecursive</a>
255+
<a class="mdl-navigation__link" href="chapter14/06-KnapSackProblemGreedy.html">06-KnapSackProblemGreedy</a>
256+
<a class="mdl-navigation__link" href="chapter14/07-LongestCommonSubsequenceDP.html">07-LongestCommonSubsequenceDP</a>
257+
<a class="mdl-navigation__link" href="chapter14/08-LongestCommonSubsequenceRecursive.html">08-LongestCommonSubsequenceRecursive</a>
258+
<a class="mdl-navigation__link" href="chapter14/09-MatrixChainMultiplicationDP.html">09-MatrixChainMultiplicationDP</a>
259+
<a class="mdl-navigation__link" href="chapter14/10-MatrixChainMultiplicationRecursive.html">10-MatrixChainMultiplicationRecursive</a>
260+
<a class="mdl-navigation__link" href="chapter14/11-RatInMaze.html">11-RatInMaze</a>
261+
<a class="mdl-navigation__link" href="chapter14/12-SudokuSolver.html">12-SudokuSolver</a>
262+
<a class="mdl-navigation__link" href="chapter14/13-IntroFunctionalProgramming.html">13-IntroFunctionalProgramming</a>
263+
</nav>
264+
</div>
265+
</div>
266+
</div>
267+
</section>
221268
</main>
222269
</div>
223270
</body>

src/js/algorithms/backtracking/rat-in-maze.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function ratInAMaze(maze) {
3434
solution[i][j] = 0;
3535
}
3636
}
37-
if (findPath(maze, 0, 0, solution) === false) {
37+
if (findPath(maze, 0, 0, solution) === true) {
3838
return solution;
3939
}
4040
return 'NO PATH FOUND';

src/js/algorithms/dynamic-programing/knapsack.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ function findValues(n, capacity, kS) {
44
// console.log('Items that are part of the solution:');
55
while (i > 0 && k > 0) {
66
if (kS[i][k] !== kS[i - 1][k]) {
7-
/* console.log(
8-
'item ' + i + ' can be part of solution w,v: ' + weights[i - 1] + ',' + values[i - 1]
9-
); */
7+
// console.log(
8+
// item ' + i + ' can be part of solution w,v: ' + weights[i - 1] + ',' + values[i - 1]
9+
// );
1010
i--;
1111
k -= kS[i][k];
1212
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util';
2+
import { quickSort } from '../sorting/quicksort';
3+
4+
function binarySearchRecursive(array, value, low, high, compareFn = defaultCompare) {
5+
if (low <= high) {
6+
const mid = Math.floor((low + high) / 2);
7+
const element = array[mid];
8+
if (compareFn(element, value) === Compare.LESS_THAN) {
9+
return binarySearchRecursive(array, value, mid + 1, high, compareFn);
10+
}
11+
if (compareFn(element, value) === Compare.BIGGER_THAN) {
12+
return binarySearchRecursive(array, value, low, mid - 1, compareFn);
13+
}
14+
return mid;
15+
}
16+
return DOES_NOT_EXIST;
17+
}
18+
19+
export function binarySearch(array, value, compareFn = defaultCompare) {
20+
const sortedArray = quickSort(array);
21+
const low = 0;
22+
const high = sortedArray.length - 1;
23+
return binarySearchRecursive(array, value, low, high, compareFn);
24+
}

src/js/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,16 @@ export { interpolationSearch } from './algorithms/search/interpolation-search';
8181
export { sequentialSearch } from './algorithms/search/sequential-search';
8282
export { findMaxValue } from './algorithms/search/min-max-search';
8383
export { findMinValue } from './algorithms/search/min-max-search';
84+
85+
// chapter 14
86+
export { binarySearch as binarySearchRecursive } from './algorithms/search/binary-search-recursive';
87+
export { minCoinChange } from './algorithms/dynamic-programing/min-coin-change';
88+
export { minCoinChange as minCoinChangeGreedy } from './algorithms/greedy/min-coin-change';
89+
export { knapSack } from './algorithms/dynamic-programing/knapsack';
90+
export { knapSack as knapSackRecursive } from './algorithms/dynamic-programing/knapsack-recursive';
91+
export { knapSack as knapSackGreedy } from './algorithms/greedy/knapsack';
92+
export { lcs } from './algorithms/dynamic-programing/longest-common-subsequence';
93+
export { lcs as lcsPrint } from './algorithms/dynamic-programing/longest-common-subsequence-print';
94+
export { lcs as lcsRecursive } from './algorithms/greedy/longest-common-subsequence';
95+
export { ratInAMaze } from './algorithms/backtracking/rat-in-maze';
96+
export { sudokuSolver } from './algorithms/backtracking/sudoku-solver';

0 commit comments

Comments
 (0)