From e36c441fa95dc1c435923503e3bc9593d2f0ff6e Mon Sep 17 00:00:00 2001 From: Bruce-Feldman Date: Wed, 4 Jul 2018 10:53:22 -0400 Subject: [PATCH] Minor fixes. (#91) * Get Bit: Make more terse * Power of two: Allowed 1 as a valid power of 2. Power of two: Removed unnecessary exception throwing. * Fisher Yates: Made more terse * Least Common Multiple: Fill undefined value * Greatest Common Divisor: Fill undefined value. Greatest Common Divisor: Make more terse. --- src/algorithms/math/bits/README.md | 7 +----- src/algorithms/math/bits/getBit.js | 2 +- .../__test__/euclieanAlgorithm.test.js | 2 +- .../euclidean-algorithm/euclideanAlgorithm.js | 25 ++----------------- .../__test__/isPowerOfTwo.test.js | 11 ++------ .../__test__/isPowerOfTwoBitwise.test.js | 11 ++------ .../math/is-power-of-two/isPowerOfTwo.js | 9 ++----- .../is-power-of-two/isPowerOfTwoBitwise.js | 9 ++----- .../leastCommonMultiple.js | 6 +---- .../sets/fisher-yates/fisherYates.js | 8 +----- 10 files changed, 15 insertions(+), 75 deletions(-) diff --git a/src/algorithms/math/bits/README.md b/src/algorithms/math/bits/README.md index 736cbebf1..05dadaba3 100644 --- a/src/algorithms/math/bits/README.md +++ b/src/algorithms/math/bits/README.md @@ -2,12 +2,7 @@ #### Get Bit -This method shifts `1` over by `bitPosition` bits, creating a -value that looks like `00100`. Then we perform `AND` operation -that clears all bits from the original number except the -`bitPosition` one. Then we compare the result with zero. If -result is zero that would mean that original number has `0` at -position `bitPosition`. +This method shifts the relevant bit to the zeroth position. Then we perform 'AND' operation with one which has bit pattern like '00001'. This clears all bits from the original number except the relevant one. If the relevant bit is one, the result is '1', otherwise the result is '0'. > See `getBit` function for further details. diff --git a/src/algorithms/math/bits/getBit.js b/src/algorithms/math/bits/getBit.js index a99ce7d53..5ff213269 100644 --- a/src/algorithms/math/bits/getBit.js +++ b/src/algorithms/math/bits/getBit.js @@ -4,5 +4,5 @@ * @return {number} */ export default function getBit(number, bitPosition) { - return (number & (1 << bitPosition)) === 0 ? 0 : 1; + return (number >> bitPosition) & 1; } diff --git a/src/algorithms/math/euclidean-algorithm/__test__/euclieanAlgorithm.test.js b/src/algorithms/math/euclidean-algorithm/__test__/euclieanAlgorithm.test.js index 01d4f4e18..ab2e3d6c6 100644 --- a/src/algorithms/math/euclidean-algorithm/__test__/euclieanAlgorithm.test.js +++ b/src/algorithms/math/euclidean-algorithm/__test__/euclieanAlgorithm.test.js @@ -2,7 +2,7 @@ import euclideanAlgorithm from '../euclideanAlgorithm'; describe('euclideanAlgorithm', () => { it('should calculate GCD', () => { - expect(euclideanAlgorithm(0, 0)).toBeNull(); + expect(euclideanAlgorithm(0, 0)).toBe(0); expect(euclideanAlgorithm(2, 0)).toBe(2); expect(euclideanAlgorithm(0, 2)).toBe(2); expect(euclideanAlgorithm(1, 2)).toBe(1); diff --git a/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js b/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js index 1532ea353..fe3b57dd6 100644 --- a/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js +++ b/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js @@ -1,32 +1,11 @@ /** * @param {number} originalA * @param {number} originalB - * @return {number|null} + * @return {number} */ export default function euclideanAlgorithm(originalA, originalB) { const a = Math.abs(originalA); const b = Math.abs(originalB); - if (a === 0 && b === 0) { - return null; - } - - if (a === 0 && b !== 0) { - return b; - } - - if (a !== 0 && b === 0) { - return a; - } - - // Normally we need to do subtraction (a - b) but to prevent - // recursion occurs to often we may shorten subtraction to (a % b). - // Since (a % b) is normally means that we've subtracted b from a - // many times until the difference became less then a. - - if (a > b) { - return euclideanAlgorithm(a % b, b); - } - - return euclideanAlgorithm(b % a, a); + return (b === 0) ? a : euclideanAlgorithm(b, a % b); } diff --git a/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js b/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js index 69058943a..f5fb4fed5 100644 --- a/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js +++ b/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js @@ -1,17 +1,10 @@ import isPowerOfTwo from '../isPowerOfTwo'; describe('isPowerOfTwo', () => { - it('should throw an exception when trying to apply function to negative number', () => { - const isNegativePowerOfTwo = () => { - isPowerOfTwo(-1); - }; - - expect(isNegativePowerOfTwo).toThrowError(); - }); - it('should check if the number is made by multiplying twos', () => { + expect(isPowerOfTwo(-1)).toBeFalsy(); expect(isPowerOfTwo(0)).toBeFalsy(); - expect(isPowerOfTwo(1)).toBeFalsy(); + expect(isPowerOfTwo(1)).toBeTruthy(); expect(isPowerOfTwo(2)).toBeTruthy(); expect(isPowerOfTwo(3)).toBeFalsy(); expect(isPowerOfTwo(4)).toBeTruthy(); diff --git a/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js b/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js index 9f6ef06e9..e092798d7 100644 --- a/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js +++ b/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js @@ -1,17 +1,10 @@ import isPowerOfTwoBitwise from '../isPowerOfTwoBitwise'; describe('isPowerOfTwoBitwise', () => { - it('should throw an exception when trying to apply function to negative number', () => { - const isNegativePowerOfTwo = () => { - isPowerOfTwoBitwise(-1); - }; - - expect(isNegativePowerOfTwo).toThrowError(); - }); - it('should check if the number is made by multiplying twos', () => { + expect(isPowerOfTwoBitwise(-1)).toBeFalsy(); expect(isPowerOfTwoBitwise(0)).toBeFalsy(); - expect(isPowerOfTwoBitwise(1)).toBeFalsy(); + expect(isPowerOfTwoBitwise(1)).toBeTruthy(); expect(isPowerOfTwoBitwise(2)).toBeTruthy(); expect(isPowerOfTwoBitwise(3)).toBeFalsy(); expect(isPowerOfTwoBitwise(4)).toBeTruthy(); diff --git a/src/algorithms/math/is-power-of-two/isPowerOfTwo.js b/src/algorithms/math/is-power-of-two/isPowerOfTwo.js index 0663b4ee5..6f7590dd8 100644 --- a/src/algorithms/math/is-power-of-two/isPowerOfTwo.js +++ b/src/algorithms/math/is-power-of-two/isPowerOfTwo.js @@ -3,13 +3,8 @@ * @return {boolean} */ export default function isPowerOfTwo(number) { - // Don't work with negative numbers. - if (number < 0) { - throw new Error('Please provide positive number'); - } - - // 0 and 1 are not powers of two. - if (number <= 1) { + // 1 (2^0) is the smallest power of two. + if (number < 1) { return false; } diff --git a/src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js b/src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js index 1433b825f..af1177355 100644 --- a/src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js +++ b/src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js @@ -3,13 +3,8 @@ * @return {boolean} */ export default function isPowerOfTwoBitwise(number) { - // Don't work with negative numbers. - if (number < 0) { - throw new Error('Please provide positive number'); - } - - // 0 and 1 are not powers of two. - if (number <= 1) { + // 1 (2^0) is the smallest power of two. + if (number < 1) { return false; } diff --git a/src/algorithms/math/least-common-multiple/leastCommonMultiple.js b/src/algorithms/math/least-common-multiple/leastCommonMultiple.js index e40f9c955..26a768103 100644 --- a/src/algorithms/math/least-common-multiple/leastCommonMultiple.js +++ b/src/algorithms/math/least-common-multiple/leastCommonMultiple.js @@ -7,9 +7,5 @@ import euclideanAlgorithm from '../euclidean-algorithm/euclideanAlgorithm'; */ export default function leastCommonMultiple(a, b) { - if (a === 0 && b === 0) { - return 0; - } - - return Math.abs(a * b) / euclideanAlgorithm(a, b); + return ((a === 0) || (b === 0)) ? 0 : Math.abs(a * b) / euclideanAlgorithm(a, b); } diff --git a/src/algorithms/sets/fisher-yates/fisherYates.js b/src/algorithms/sets/fisher-yates/fisherYates.js index 98cb2c31d..23b710b9f 100644 --- a/src/algorithms/sets/fisher-yates/fisherYates.js +++ b/src/algorithms/sets/fisher-yates/fisherYates.js @@ -6,15 +6,9 @@ export default function fisherYates(originalArray) { // Clone array from preventing original array from modification (for testing purpose). const array = originalArray.slice(0); - if (array.length <= 1) { - return array; - } - for (let i = (array.length - 1); i > 0; i -= 1) { const randomIndex = Math.floor(Math.random() * (i + 1)); - const tmp = array[randomIndex]; - array[randomIndex] = array[i]; - array[i] = tmp; + [array[i], array[randomIndex]] = [array[randomIndex], array[i]]; } return array;