From 677146717da47971703c9a6dd850b569f3026b52 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 17 Oct 2020 19:54:34 +0530 Subject: [PATCH 1/4] Typo Fix in BFS test --- .../breadth-first-search/__test__/breadthFirstSearch.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/tree/breadth-first-search/__test__/breadthFirstSearch.test.js b/src/algorithms/tree/breadth-first-search/__test__/breadthFirstSearch.test.js index 4a77f0f3..2bf653a9 100644 --- a/src/algorithms/tree/breadth-first-search/__test__/breadthFirstSearch.test.js +++ b/src/algorithms/tree/breadth-first-search/__test__/breadthFirstSearch.test.js @@ -2,7 +2,7 @@ import BinaryTreeNode from '../../../../data-structures/tree/BinaryTreeNode'; import breadthFirstSearch from '../breadthFirstSearch'; describe('breadthFirstSearch', () => { - it('should perform DFS operation on tree', () => { + it('should perform BFS operation on tree', () => { const nodeA = new BinaryTreeNode('A'); const nodeB = new BinaryTreeNode('B'); const nodeC = new BinaryTreeNode('C'); From b5ca4b12e8b33a70a34e7277b8d7edd0bbb5faf9 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 17 Oct 2020 20:06:20 +0530 Subject: [PATCH 2/4] Added Mini-Max sum Algorithm --- README.md | 1 + src/algorithms/math/mini-max/MiniMax.js | 27 +++++++++++++++++++ src/algorithms/math/mini-max/Readme.md | 12 +++++++++ .../math/mini-max/__test__/miniMax.test.js | 13 +++++++++ 4 files changed, 53 insertions(+) create mode 100644 src/algorithms/math/mini-max/MiniMax.js create mode 100644 src/algorithms/math/mini-max/Readme.md create mode 100644 src/algorithms/math/mini-max/__test__/miniMax.test.js diff --git a/README.md b/README.md index 2a2c50ae..fd2e52ff 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ a set of rules that precisely define a sequence of operations. * `B` [Pascal's Triangle](src/algorithms/math/pascal-triangle) * `B` [Complex Number](src/algorithms/math/complex-number) - complex numbers and basic operations with them * `B` [Radian & Degree](src/algorithms/math/radian) - radians to degree and backwards conversion + * `B` [Mini-Max Sum](src/algorithms/math/mini-max-sum) * `B` [Fast Powering](src/algorithms/math/fast-powering) * `A` [Integer Partition](src/algorithms/math/integer-partition) * `A` [Square Root](src/algorithms/math/square-root) - Newton's method diff --git a/src/algorithms/math/mini-max/MiniMax.js b/src/algorithms/math/mini-max/MiniMax.js new file mode 100644 index 00000000..f6a1c47e --- /dev/null +++ b/src/algorithms/math/mini-max/MiniMax.js @@ -0,0 +1,27 @@ + +/** + * + * @param numbers - array of numbers + * @return [number - Max, number - Min] + */ +export default function miniMaxSum(numbers) { + if (!Array.isArray(numbers) || numbers.length === 0) { + return [null, null]; + } + + let min = numbers[0]; + let max = numbers[0]; + let sum = numbers[0]; + + for (let i = 1; i < numbers.length; i += 1) { + sum += numbers[i]; + + if (numbers[i] > max) { + max = numbers[i]; + } else if (numbers[i] < min) { + min = numbers[i]; + } + } + + return [sum - max, sum - min]; + } \ No newline at end of file diff --git a/src/algorithms/math/mini-max/Readme.md b/src/algorithms/math/mini-max/Readme.md new file mode 100644 index 00000000..2a2fc116 --- /dev/null +++ b/src/algorithms/math/mini-max/Readme.md @@ -0,0 +1,12 @@ + +# Mini-Max Sum + +Given an array of five positive integers, find the minimum and maximum values that +can be calculated by summing exactly four of the array integers. Then return the +respective minimum and maximum values as a array of numbers + + +``` +[1, 2, 3, 4, 5] = [10, 14] +[1, 3, 5, 7, 9] = [16, 24] +``` \ No newline at end of file diff --git a/src/algorithms/math/mini-max/__test__/miniMax.test.js b/src/algorithms/math/mini-max/__test__/miniMax.test.js new file mode 100644 index 00000000..e0059876 --- /dev/null +++ b/src/algorithms/math/mini-max/__test__/miniMax.test.js @@ -0,0 +1,13 @@ +import miniMaxSum from '../miniMaxSum'; + +describe('miniMaxSum', () => { + it('should calculate miniMaxSum', () => { + expect(miniMaxSum([1, 3, 5, 7, 9])).toStrictEqual([16, 24]); + expect(miniMaxSum([1, 2, 3, 4, 5])).toStrictEqual([10, 14]); + expect(miniMaxSum([-1, -2, 3, 4, 5])).toStrictEqual([4, 11]); + expect(miniMaxSum([0, 1, 2, 3, 4, 5])).toStrictEqual([10, 15]); + expect(miniMaxSum([])).toStrictEqual([null, null]); + expect(miniMaxSum(12)).toStrictEqual([null, null]); + expect(miniMaxSum(null)).toStrictEqual([null, null]); + }); +}); \ No newline at end of file From ceed279de850dbf34f08d49d0852b6564c748df2 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 17 Oct 2020 20:16:03 +0530 Subject: [PATCH 3/4] Remove Array check and optimise --- .../sorting/insertion-sort/InsertionSort.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/algorithms/sorting/insertion-sort/InsertionSort.js b/src/algorithms/sorting/insertion-sort/InsertionSort.js index aa447542..3f7bd55c 100644 --- a/src/algorithms/sorting/insertion-sort/InsertionSort.js +++ b/src/algorithms/sorting/insertion-sort/InsertionSort.js @@ -8,32 +8,30 @@ export default class InsertionSort extends Sort { for (let i = 0; i < array.length; i += 1) { let currentIndex = i; + const temp = array[currentIndex]; + // Call visiting callback. this.callbacks.visitingCallback(array[i]); // Go and check if previous elements and greater then current one. // If this is the case then swap that elements. while ( - array[currentIndex - 1] !== undefined - && this.comparator.lessThan(array[currentIndex], array[currentIndex - 1]) + currentIndex > 0 + && this.comparator.lessThan(temp, array[currentIndex - 1]) ) { // Call visiting callback. this.callbacks.visitingCallback(array[currentIndex - 1]); // Swap the elements. - [ - array[currentIndex - 1], - array[currentIndex], - ] = [ - array[currentIndex], - array[currentIndex - 1], - ]; + array[currentIndex] = array[currentIndex - 1]; // Shift current index left. currentIndex -= 1; } + + array[currentIndex] = temp; } return array; } -} +} \ No newline at end of file From 45762593b532f51b3404bd43d3141bfba2db8fc3 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 17 Oct 2020 20:30:01 +0530 Subject: [PATCH 4/4] Added Linear Search Fix --- .../__test__/linearSearch.test.js | 20 +++++++++---------- .../search/linear-search/linearSearch.js | 13 ++++++------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/algorithms/search/linear-search/__test__/linearSearch.test.js b/src/algorithms/search/linear-search/__test__/linearSearch.test.js index c578fd1f..bf47aff8 100644 --- a/src/algorithms/search/linear-search/__test__/linearSearch.test.js +++ b/src/algorithms/search/linear-search/__test__/linearSearch.test.js @@ -4,17 +4,17 @@ describe('linearSearch', () => { it('should search all numbers in array', () => { const array = [1, 2, 4, 6, 2]; - expect(linearSearch(array, 10)).toEqual([]); - expect(linearSearch(array, 1)).toEqual([0]); - expect(linearSearch(array, 2)).toEqual([1, 4]); + expect(linearSearch(array, 10)).toEqual(-1); + expect(linearSearch(array, 1)).toEqual(0); + expect(linearSearch(array, 2)).toEqual(1); }); it('should search all strings in array', () => { const array = ['a', 'b', 'a']; - expect(linearSearch(array, 'c')).toEqual([]); - expect(linearSearch(array, 'b')).toEqual([1]); - expect(linearSearch(array, 'a')).toEqual([0, 2]); + expect(linearSearch(array, 'c')).toEqual(-1); + expect(linearSearch(array, 'b')).toEqual(1); + expect(linearSearch(array, 'a')).toEqual(0); }); it('should search through objects as well', () => { @@ -33,8 +33,8 @@ describe('linearSearch', () => { { key: 6 }, ]; - expect(linearSearch(array, { key: 10 }, comparatorCallback)).toEqual([]); - expect(linearSearch(array, { key: 5 }, comparatorCallback)).toEqual([0]); - expect(linearSearch(array, { key: 6 }, comparatorCallback)).toEqual([1, 3]); + expect(linearSearch(array, { key: 10 }, comparatorCallback)).toEqual(-1); + expect(linearSearch(array, { key: 5 }, comparatorCallback)).toEqual(0); + expect(linearSearch(array, { key: 6 }, comparatorCallback)).toEqual(1); }); -}); +}); \ No newline at end of file diff --git a/src/algorithms/search/linear-search/linearSearch.js b/src/algorithms/search/linear-search/linearSearch.js index c5fb67c7..227e4885 100644 --- a/src/algorithms/search/linear-search/linearSearch.js +++ b/src/algorithms/search/linear-search/linearSearch.js @@ -10,13 +10,12 @@ import Comparator from '../../../utils/comparator/Comparator'; */ export default function linearSearch(array, seekElement, comparatorCallback) { const comparator = new Comparator(comparatorCallback); - const foundIndices = []; - array.forEach((element, index) => { - if (comparator.equal(element, seekElement)) { - foundIndices.push(index); + for (let i = 0; i < array.length; i += 1) { + if (comparator.equal(array[i], seekElement)) { + return i; } - }); + } - return foundIndices; -} + return -1; +} \ No newline at end of file