From 5b49d8a8a12712328a8de9b94eba949d11553686 Mon Sep 17 00:00:00 2001 From: Gabriela Date: Thu, 19 May 2022 14:53:34 +0200 Subject: [PATCH 1/4] fix(runtimes/02-binary-search):fixes binary search iterative --- src/runtimes/02-binary-search.js | 44 ++++++--------------------- src/runtimes/02-binary-search.spec.js | 42 ++++++++++++++++++++----- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/runtimes/02-binary-search.js b/src/runtimes/02-binary-search.js index 4e241b85..76203d31 100644 --- a/src/runtimes/02-binary-search.js +++ b/src/runtimes/02-binary-search.js @@ -38,50 +38,24 @@ function binarySearchRecursive(array, search, offset = 0) { * @param {string|number} search value to search for */ function binarySearchIterative(array, search) { - // console.log('binarySearchIterative', {array, search}); let start = 0; - let end = array.length; - const half = () => parseInt((end - start) / 2, 10) + start; + let end = array.length - 1; + const half = () => start + parseInt((end - start) / 2, 10); - while (end - start > 0) { + while (start <= end) { const currentIndex = half(); const current = array[currentIndex]; - if (current === search) { - return currentIndex; - } if (search > current) { - start = currentIndex; + if (current === search) return currentIndex; + + if (search > current) { + start = currentIndex + 1; } else if (search < current) { - end = currentIndex; + end = currentIndex - 1; } } return -1; } -// const binarySearch = binarySearchRecursive; -const binarySearch = binarySearchIterative; - -// function test() { -// const directory = ['Adrian', 'Bella', 'Charlotte', 'Daniel', -// 'Emma', 'Hanna', 'Isabella', 'Jayden', 'Kaylee', 'Luke', 'Mia', -// 'Nora', 'Olivia', 'Paisley', 'Riley', 'Thomas', 'Wyatt', 'Xander', 'Zoe']; -// -// const assert = require('assert'); -// assert.equal(binarySearch([], 'not found'), -1); -// assert.equal(binarySearch([1], 2), -1); -// assert.equal(binarySearch([1], 1), 0); -// assert.equal(binarySearch([1, 2, 3], 1), 0); -// assert.equal(binarySearch([1, 2, 3], 2), 1); -// assert.equal(binarySearch([1, 2, 3], 3), 2); -// assert.equal(binarySearch([1, 2, 3], 31), -1); -// assert.equal(binarySearch(directory, 'Adrian'), 0); -// assert.equal(binarySearch(directory, 'Hanna'), 5); -// assert.equal(binarySearch(directory, 'Zoe'), 18); -// assert.equal(binarySearch(directory, 'not found'), -1); -// } - -// test(); - - -module.exports = { binarySearch, binarySearchIterative, binarySearchRecursive }; +module.exports = { binarySearchIterative, binarySearchRecursive }; diff --git a/src/runtimes/02-binary-search.spec.js b/src/runtimes/02-binary-search.spec.js index 4850cd7c..e6aca91b 100644 --- a/src/runtimes/02-binary-search.spec.js +++ b/src/runtimes/02-binary-search.spec.js @@ -1,6 +1,6 @@ -const binarySearch = require('./02-binary-search').binarySearchRecursive; +const { binarySearchRecursive, binarySearchIterative } = require('./02-binary-search'); -describe('Binary Search', () => { +describe('Binary Search Recursive', () => { let array; beforeEach(() => { @@ -8,22 +8,50 @@ describe('Binary Search', () => { }); it('should find a middle element', () => { - expect(binarySearch(array, 9)).toEqual(1); + expect(binarySearchRecursive(array, 9)).toEqual(1); }); it('should find an first element', () => { - expect(binarySearch(array, 7)).toEqual(0); + expect(binarySearchRecursive(array, 7)).toEqual(0); }); it('should find the last element', () => { - expect(binarySearch(array, 23)).toEqual(3); + expect(binarySearchRecursive(array, 23)).toEqual(3); }); it('should not find an bigger element', () => { - expect(binarySearch(array, 9000)).toEqual(-1); + expect(binarySearchRecursive(array, 9000)).toEqual(-1); }); it('should find a smaller element', () => { - expect(binarySearch(array, -9)).toEqual(-1); + expect(binarySearchRecursive(array, -9)).toEqual(-1); + }); +}); + +describe('Binary Search Iterative', () => { + let array; + + beforeEach(() => { + array = [7, 9, 13, 23]; + }); + + it('should find a middle element', () => { + expect(binarySearchIterative(array, 9)).toEqual(1); + }); + + it('should find an first element', () => { + expect(binarySearchIterative(array, 7)).toEqual(0); + }); + + it('should find the last element', () => { + expect(binarySearchIterative(array, 23)).toEqual(3); + }); + + it('should not find an bigger element', () => { + expect(binarySearchIterative(array, 9000)).toEqual(-1); + }); + + it('should find a smaller element', () => { + expect(binarySearchIterative(array, -9)).toEqual(-1); }); }); From cde6e8a2291bb97e08b28dedf093942e664eaa09 Mon Sep 17 00:00:00 2001 From: Balaji Saravanan Date: Sun, 29 May 2022 12:01:08 +0530 Subject: [PATCH 2/4] Update heap.js Fix proper parent index fetching --- src/data-structures/heaps/heap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data-structures/heaps/heap.js b/src/data-structures/heaps/heap.js index 54863c39..537894ee 100644 --- a/src/data-structures/heaps/heap.js +++ b/src/data-structures/heaps/heap.js @@ -59,7 +59,7 @@ class Heap { */ bubbleUp() { let index = this.size - 1; - const parent = (i) => Math.ceil(i / 2 - 1); + const parent = (i) => Math.ceil(i / 2) - 1; while (parent(index) >= 0 && this.comparator(parent(index), index) > 0) { this.swap(parent(index), index); index = parent(index); From c9249d38cc3809bc2c0457f35494772cb57b28b9 Mon Sep 17 00:00:00 2001 From: Adrian Mejia Date: Sat, 10 Dec 2022 16:02:51 -0500 Subject: [PATCH 3/4] chore(binary-search): consolidate test for multiple implementations --- src/runtimes/02-binary-search.js | 6 +- src/runtimes/02-binary-search.spec.js | 92 +++++++++++---------------- 2 files changed, 41 insertions(+), 57 deletions(-) diff --git a/src/runtimes/02-binary-search.js b/src/runtimes/02-binary-search.js index 76203d31..c8118160 100644 --- a/src/runtimes/02-binary-search.js +++ b/src/runtimes/02-binary-search.js @@ -18,9 +18,11 @@ function binarySearchRecursive(array, search, offset = 0) { if (current === search) { return offset + half; - } if (array.length === 1) { + } + if (array.length === 1) { return -1; - } if (search > current) { + } + if (search > current) { const right = array.slice(half); return binarySearchRecursive(right, search, offset + half); } diff --git a/src/runtimes/02-binary-search.spec.js b/src/runtimes/02-binary-search.spec.js index e6aca91b..1e8f8f56 100644 --- a/src/runtimes/02-binary-search.spec.js +++ b/src/runtimes/02-binary-search.spec.js @@ -1,57 +1,39 @@ -const { binarySearchRecursive, binarySearchIterative } = require('./02-binary-search'); - -describe('Binary Search Recursive', () => { - let array; - - beforeEach(() => { - array = [7, 9, 13, 23]; - }); - - it('should find a middle element', () => { - expect(binarySearchRecursive(array, 9)).toEqual(1); - }); - - it('should find an first element', () => { - expect(binarySearchRecursive(array, 7)).toEqual(0); - }); - - it('should find the last element', () => { - expect(binarySearchRecursive(array, 23)).toEqual(3); - }); - - it('should not find an bigger element', () => { - expect(binarySearchRecursive(array, 9000)).toEqual(-1); - }); - - it('should find a smaller element', () => { - expect(binarySearchRecursive(array, -9)).toEqual(-1); - }); -}); - -describe('Binary Search Iterative', () => { - let array; - - beforeEach(() => { - array = [7, 9, 13, 23]; - }); - - it('should find a middle element', () => { - expect(binarySearchIterative(array, 9)).toEqual(1); - }); - - it('should find an first element', () => { - expect(binarySearchIterative(array, 7)).toEqual(0); - }); - - it('should find the last element', () => { - expect(binarySearchIterative(array, 23)).toEqual(3); - }); - - it('should not find an bigger element', () => { - expect(binarySearchIterative(array, 9000)).toEqual(-1); - }); - - it('should find a smaller element', () => { - expect(binarySearchIterative(array, -9)).toEqual(-1); +const { + binarySearchRecursive, + binarySearchIterative, +} = require("./02-binary-search"); + +const binarySearchImplementations = [ + binarySearchRecursive, + binarySearchIterative, +]; + +binarySearchImplementations.forEach((binarySearchImpl) => { + describe(binarySearchImpl.name, () => { + let array; + + beforeEach(() => { + array = [7, 9, 13, 23]; + }); + + it("should find a middle element", () => { + expect(binarySearchImpl(array, 9)).toEqual(1); + }); + + it("should find an first element", () => { + expect(binarySearchImpl(array, 7)).toEqual(0); + }); + + it("should find the last element", () => { + expect(binarySearchImpl(array, 23)).toEqual(3); + }); + + it("should not find an bigger element", () => { + expect(binarySearchImpl(array, 9000)).toEqual(-1); + }); + + it("should find a smaller element", () => { + expect(binarySearchImpl(array, -9)).toEqual(-1); + }); }); }); From fefb06a6b51a7699cb3986ed2d20dc0d03511055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ca=C3=ADque=20de=20Castro=20Soares=20da=20Silva?= Date: Mon, 26 Dec 2022 17:45:05 -0300 Subject: [PATCH 4/4] Fix return of unshift method Fix return of unshift method --- book/content/part02/array.asc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/content/part02/array.asc b/book/content/part02/array.asc index 3bde2929..25c029ee 100644 --- a/book/content/part02/array.asc +++ b/book/content/part02/array.asc @@ -73,7 +73,7 @@ Here's an example: [source, javascript] ---- const array = [2, 5, 1]; -array.unshift(0); // ↪️ 8 +array.unshift(0); // ↪️ 4 console.log(array); // [ 0, 2, 5, 1 ] array.unshift(-2, -1); // ↪️ 6 console.log(array); // [ -2, -1, 0, 2, 5, 1 ]