Skip to content

Third edition #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ coverage
coverage.lcov
mochawesome-report/*
dist/js/*
dist-/*
dist/ts/*
snippet.js
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Learning JavaScript Data Structures and Algorithms
====================================

[![Build Status](https://travis-ci.org/loiane/javascript-datastructures-algorithms.svg?branch=third-edition)](https://travis-ci.org/loiane/javascript-datastructures-algorithms)
[![codecov](https://codecov.io/gh/loiane/javascript-datastructures-algorithms/branch/third-edition/graph/badge.svg)](https://codecov.io/gh/loiane/javascript-datastructures-algorithms)
[![codecov](https://codecov.io/gh/loiane/javascript-datastructures-algorithms/branch/master/graph/badge.svg)](https://codecov.io/gh/loiane/javascript-datastructures-algorithms)
[![devDependencies Status](https://david-dm.org/loiane/javascript-datastructures-algorithms/dev-status.svg)](https://david-dm.org/loiane/javascript-datastructures-algorithms?type=dev)
[![dependencies Status](https://david-dm.org/loiane/javascript-datastructures-algorithms/status.svg)](https://david-dm.org/loiane/javascript-datastructures-algorithms)
[![Greenkeeper badge](https://badges.greenkeeper.io/loiane/javascript-datastructures-algorithms.svg)](https://greenkeeper.io/)
Expand Down
2 changes: 1 addition & 1 deletion examples/PacktDataStructuresAlgorithms.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/PacktDataStructuresAlgorithms.min.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions examples/chapter14/01-DC-BinarySearch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./01-DC-BinarySearch.js"></script>
</body>
</html>
Empty file.
11 changes: 11 additions & 0 deletions examples/chapter14/02-MinCoinChangeDP.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./02-MinCoinChangeDP.js"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions examples/chapter14/02-MinCoinChangeDP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { minCoinChange } = PacktDataStructuresAlgorithms;

console.log(minCoinChange([1, 5, 10], 15)); // [5, 10]
console.log(minCoinChange([1, 3, 4], 6)); // [3, 3]

11 changes: 11 additions & 0 deletions examples/chapter14/03-MinCoinChangeGreedy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./03-MinCoinChangeGreedy.js"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions examples/chapter14/03-MinCoinChangeGreedy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { minCoinChangeGreedy } = PacktDataStructuresAlgorithms;

console.log(minCoinChangeGreedy([1, 5, 10], 15)); // [5, 10]
console.log(minCoinChangeGreedy([1, 3, 4], 6)); // [4, 1, 1]

11 changes: 11 additions & 0 deletions examples/chapter14/04-KnapsackProblemDP.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./04-KnapsackProblemDP.js"></script>
</body>
</html>
8 changes: 8 additions & 0 deletions examples/chapter14/04-KnapsackProblemDP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { knapSack } = PacktDataStructuresAlgorithms;

const values = [3,4,5];
const weights = [2,3,4];
const capacity = 5;
const n = values.length;

console.log(knapSack(capacity, weights, values, n));
11 changes: 11 additions & 0 deletions examples/chapter14/05-KnapSackProblemRecursive.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./05-KnapSackProblemRecursive.js"></script>
</body>
</html>
8 changes: 8 additions & 0 deletions examples/chapter14/05-KnapSackProblemRecursive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { knapSackRecursive } = PacktDataStructuresAlgorithms;

const values = [3,4,5];
const weights = [2,3,4];
const capacity = 5;
const n = values.length;

console.log(knapSackRecursive(capacity, weights, values, n));
11 changes: 11 additions & 0 deletions examples/chapter14/06-KnapSackProblemGreedy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./06-KnapSackProblemGreedy.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions examples/chapter14/06-KnapSackProblemGreedy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { knapSackGreedy } = PacktDataStructuresAlgorithms;

const values = [3,4,5];
const weights = [2,3,4];
const capacity = 5;

console.log(knapSackGreedy(capacity, weights, values));
11 changes: 11 additions & 0 deletions examples/chapter14/07-LongestCommonSubsequenceDP.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./07-LongestCommonSubsequenceDP.js"></script>
</body>
</html>
Empty file.
11 changes: 11 additions & 0 deletions examples/chapter14/08-LongestCommonSubsequenceRecursive.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./08-LongestCommonSubsequenceRecursive.js"></script>
</body>
</html>
Empty file.
11 changes: 11 additions & 0 deletions examples/chapter14/09-MatrixChainMultiplicationDP.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./09-MatrixChainMultiplicationDP.js"></script>
</body>
</html>
Empty file.
11 changes: 11 additions & 0 deletions examples/chapter14/10-MatrixChainMultiplicationRecursive.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./10-MatrixChainMultiplicationRecursive.js"></script>
</body>
</html>
Empty file.
11 changes: 11 additions & 0 deletions examples/chapter14/11-RatInMaze.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./11-RatInMaze.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions examples/chapter14/11-RatInMaze.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { ratInAMaze } = PacktDataStructuresAlgorithms;

const maze = [
[1, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 1, 0],
[0, 1, 1, 1]
];

console.log(ratInAMaze(maze));
11 changes: 11 additions & 0 deletions examples/chapter14/12-SudokuSolver.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./12-SudokuSolver.js"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions examples/chapter14/12-SudokuSolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { sudokuSolver } = PacktDataStructuresAlgorithms;

const sudokuGrid = [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9]
];

console.log(sudokuSolver(sudokuGrid));
11 changes: 11 additions & 0 deletions examples/chapter14/13-IntroFunctionalProgramming.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
<script src="./13-IntroFunctionalProgramming.js"></script>
</body>
</html>
Empty file.
47 changes: 47 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<a href="#scroll-tab-9" class="mdl-layout__tab">09</a>
<a href="#scroll-tab-10" class="mdl-layout__tab">10</a>
<a href="#scroll-tab-11" class="mdl-layout__tab">11</a>
<a href="#scroll-tab-12" class="mdl-layout__tab">12</a>
<a href="#scroll-tab-13" class="mdl-layout__tab">13</a>
<a href="#scroll-tab-14" class="mdl-layout__tab">14</a>
<a href="#scroll-tab-15" class="mdl-layout__tab">15</a>
</div>
</header>
<main class="mdl-layout__content">
Expand Down Expand Up @@ -218,6 +222,49 @@
</div>
</div>
</section>
<section class="mdl-layout__tab-panel" id="scroll-tab-12">
<div class="page-content">
<div class="page-content mdl-layout--fixed-drawer">
<div class="mdl-layout__drawer is-visible">
<nav class="mdl-navigation">
</nav>
</div>
</div>
</div>
</section>
<section class="mdl-layout__tab-panel" id="scroll-tab-13">
<div class="page-content">
<div class="page-content mdl-layout--fixed-drawer">
<div class="mdl-layout__drawer is-visible">
<nav class="mdl-navigation">
</nav>
</div>
</div>
</div>
</section>
<section class="mdl-layout__tab-panel" id="scroll-tab-14">
<div class="page-content">
<div class="page-content mdl-layout--fixed-drawer">
<div class="mdl-layout__drawer is-visible">
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="chapter14/01-DC-BinarySearch.html">01-DC-BinarySearch</a>
<a class="mdl-navigation__link" href="chapter14/02-MinCoinChangeDP.html">02-MinCoinChangeDP</a>
<a class="mdl-navigation__link" href="chapter14/03-MinCoinChangeGreedy.html">03-MinCoinChangeGreedy</a>
<a class="mdl-navigation__link" href="chapter14/04-KnapsackProblemDP.html">04-KnapsackProblemDP</a>
<a class="mdl-navigation__link" href="chapter14/05-KnapSackProblemRecursive.html">05-KnapSackProblemRecursive</a>
<a class="mdl-navigation__link" href="chapter14/06-KnapSackProblemGreedy.html">06-KnapSackProblemGreedy</a>
<a class="mdl-navigation__link" href="chapter14/07-LongestCommonSubsequenceDP.html">07-LongestCommonSubsequenceDP</a>
<a class="mdl-navigation__link" href="chapter14/08-LongestCommonSubsequenceRecursive.html">08-LongestCommonSubsequenceRecursive</a>
<a class="mdl-navigation__link" href="chapter14/09-MatrixChainMultiplicationDP.html">09-MatrixChainMultiplicationDP</a>
<a class="mdl-navigation__link" href="chapter14/10-MatrixChainMultiplicationRecursive.html">10-MatrixChainMultiplicationRecursive</a>
<a class="mdl-navigation__link" href="chapter14/11-RatInMaze.html">11-RatInMaze</a>
<a class="mdl-navigation__link" href="chapter14/12-SudokuSolver.html">12-SudokuSolver</a>
<a class="mdl-navigation__link" href="chapter14/13-IntroFunctionalProgramming.html">13-IntroFunctionalProgramming</a>
</nav>
</div>
</div>
</div>
</section>
</main>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion src/js/algorithms/backtracking/rat-in-maze.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function ratInAMaze(maze) {
solution[i][j] = 0;
}
}
if (findPath(maze, 0, 0, solution) === false) {
if (findPath(maze, 0, 0, solution) === true) {
return solution;
}
return 'NO PATH FOUND';
Expand Down
6 changes: 3 additions & 3 deletions src/js/algorithms/dynamic-programing/knapsack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ function findValues(n, capacity, kS) {
// console.log('Items that are part of the solution:');
while (i > 0 && k > 0) {
if (kS[i][k] !== kS[i - 1][k]) {
/* console.log(
'item ' + i + ' can be part of solution w,v: ' + weights[i - 1] + ',' + values[i - 1]
); */
// console.log(
// item ' + i + ' can be part of solution w,v: ' + weights[i - 1] + ',' + values[i - 1]
// );
i--;
k -= kS[i][k];
} else {
Expand Down
24 changes: 24 additions & 0 deletions src/js/algorithms/search/binary-search-recursive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util';
import { quickSort } from '../sorting/quicksort';

function binarySearchRecursive(array, value, low, high, compareFn = defaultCompare) {
if (low <= high) {
const mid = Math.floor((low + high) / 2);
const element = array[mid];
if (compareFn(element, value) === Compare.LESS_THAN) {
return binarySearchRecursive(array, value, mid + 1, high, compareFn);
}
if (compareFn(element, value) === Compare.BIGGER_THAN) {
return binarySearchRecursive(array, value, low, mid - 1, compareFn);
}
return mid;
}
return DOES_NOT_EXIST;
}

export function binarySearch(array, value, compareFn = defaultCompare) {
const sortedArray = quickSort(array);
const low = 0;
const high = sortedArray.length - 1;
return binarySearchRecursive(array, value, low, high, compareFn);
}
13 changes: 13 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,16 @@ export { interpolationSearch } from './algorithms/search/interpolation-search';
export { sequentialSearch } from './algorithms/search/sequential-search';
export { findMaxValue } from './algorithms/search/min-max-search';
export { findMinValue } from './algorithms/search/min-max-search';

// chapter 14
export { binarySearch as binarySearchRecursive } from './algorithms/search/binary-search-recursive';
export { minCoinChange } from './algorithms/dynamic-programing/min-coin-change';
export { minCoinChange as minCoinChangeGreedy } from './algorithms/greedy/min-coin-change';
export { knapSack } from './algorithms/dynamic-programing/knapsack';
export { knapSack as knapSackRecursive } from './algorithms/dynamic-programing/knapsack-recursive';
export { knapSack as knapSackGreedy } from './algorithms/greedy/knapsack';
export { lcs } from './algorithms/dynamic-programing/longest-common-subsequence';
export { lcs as lcsPrint } from './algorithms/dynamic-programing/longest-common-subsequence-print';
export { lcs as lcsRecursive } from './algorithms/greedy/longest-common-subsequence';
export { ratInAMaze } from './algorithms/backtracking/rat-in-maze';
export { sudokuSolver } from './algorithms/backtracking/sudoku-solver';