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.
This commit is contained in:
Bruce-Feldman
2018-07-04 10:53:22 -04:00
committed by Oleksii Trekhleb
parent 93bfe97e27
commit e36c441fa9
10 changed files with 15 additions and 75 deletions

View File

@ -2,12 +2,7 @@
#### Get Bit #### Get Bit
This method shifts `1` over by `bitPosition` bits, creating a 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'.
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`.
> See `getBit` function for further details. > See `getBit` function for further details.

View File

@ -4,5 +4,5 @@
* @return {number} * @return {number}
*/ */
export default function getBit(number, bitPosition) { export default function getBit(number, bitPosition) {
return (number & (1 << bitPosition)) === 0 ? 0 : 1; return (number >> bitPosition) & 1;
} }

View File

@ -2,7 +2,7 @@ import euclideanAlgorithm from '../euclideanAlgorithm';
describe('euclideanAlgorithm', () => { describe('euclideanAlgorithm', () => {
it('should calculate GCD', () => { it('should calculate GCD', () => {
expect(euclideanAlgorithm(0, 0)).toBeNull(); expect(euclideanAlgorithm(0, 0)).toBe(0);
expect(euclideanAlgorithm(2, 0)).toBe(2); expect(euclideanAlgorithm(2, 0)).toBe(2);
expect(euclideanAlgorithm(0, 2)).toBe(2); expect(euclideanAlgorithm(0, 2)).toBe(2);
expect(euclideanAlgorithm(1, 2)).toBe(1); expect(euclideanAlgorithm(1, 2)).toBe(1);

View File

@ -1,32 +1,11 @@
/** /**
* @param {number} originalA * @param {number} originalA
* @param {number} originalB * @param {number} originalB
* @return {number|null} * @return {number}
*/ */
export default function euclideanAlgorithm(originalA, originalB) { export default function euclideanAlgorithm(originalA, originalB) {
const a = Math.abs(originalA); const a = Math.abs(originalA);
const b = Math.abs(originalB); const b = Math.abs(originalB);
if (a === 0 && b === 0) { return (b === 0) ? a : euclideanAlgorithm(b, a % b);
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);
} }

View File

@ -1,17 +1,10 @@
import isPowerOfTwo from '../isPowerOfTwo'; import isPowerOfTwo from '../isPowerOfTwo';
describe('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', () => { it('should check if the number is made by multiplying twos', () => {
expect(isPowerOfTwo(-1)).toBeFalsy();
expect(isPowerOfTwo(0)).toBeFalsy(); expect(isPowerOfTwo(0)).toBeFalsy();
expect(isPowerOfTwo(1)).toBeFalsy(); expect(isPowerOfTwo(1)).toBeTruthy();
expect(isPowerOfTwo(2)).toBeTruthy(); expect(isPowerOfTwo(2)).toBeTruthy();
expect(isPowerOfTwo(3)).toBeFalsy(); expect(isPowerOfTwo(3)).toBeFalsy();
expect(isPowerOfTwo(4)).toBeTruthy(); expect(isPowerOfTwo(4)).toBeTruthy();

View File

@ -1,17 +1,10 @@
import isPowerOfTwoBitwise from '../isPowerOfTwoBitwise'; import isPowerOfTwoBitwise from '../isPowerOfTwoBitwise';
describe('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', () => { it('should check if the number is made by multiplying twos', () => {
expect(isPowerOfTwoBitwise(-1)).toBeFalsy();
expect(isPowerOfTwoBitwise(0)).toBeFalsy(); expect(isPowerOfTwoBitwise(0)).toBeFalsy();
expect(isPowerOfTwoBitwise(1)).toBeFalsy(); expect(isPowerOfTwoBitwise(1)).toBeTruthy();
expect(isPowerOfTwoBitwise(2)).toBeTruthy(); expect(isPowerOfTwoBitwise(2)).toBeTruthy();
expect(isPowerOfTwoBitwise(3)).toBeFalsy(); expect(isPowerOfTwoBitwise(3)).toBeFalsy();
expect(isPowerOfTwoBitwise(4)).toBeTruthy(); expect(isPowerOfTwoBitwise(4)).toBeTruthy();

View File

@ -3,13 +3,8 @@
* @return {boolean} * @return {boolean}
*/ */
export default function isPowerOfTwo(number) { export default function isPowerOfTwo(number) {
// Don't work with negative numbers. // 1 (2^0) is the smallest power of two.
if (number < 0) { if (number < 1) {
throw new Error('Please provide positive number');
}
// 0 and 1 are not powers of two.
if (number <= 1) {
return false; return false;
} }

View File

@ -3,13 +3,8 @@
* @return {boolean} * @return {boolean}
*/ */
export default function isPowerOfTwoBitwise(number) { export default function isPowerOfTwoBitwise(number) {
// Don't work with negative numbers. // 1 (2^0) is the smallest power of two.
if (number < 0) { if (number < 1) {
throw new Error('Please provide positive number');
}
// 0 and 1 are not powers of two.
if (number <= 1) {
return false; return false;
} }

View File

@ -7,9 +7,5 @@ import euclideanAlgorithm from '../euclidean-algorithm/euclideanAlgorithm';
*/ */
export default function leastCommonMultiple(a, b) { export default function leastCommonMultiple(a, b) {
if (a === 0 && b === 0) { return ((a === 0) || (b === 0)) ? 0 : Math.abs(a * b) / euclideanAlgorithm(a, b);
return 0;
}
return Math.abs(a * b) / euclideanAlgorithm(a, b);
} }

View File

@ -6,15 +6,9 @@ export default function fisherYates(originalArray) {
// Clone array from preventing original array from modification (for testing purpose). // Clone array from preventing original array from modification (for testing purpose).
const array = originalArray.slice(0); const array = originalArray.slice(0);
if (array.length <= 1) {
return array;
}
for (let i = (array.length - 1); i > 0; i -= 1) { for (let i = (array.length - 1); i > 0; i -= 1) {
const randomIndex = Math.floor(Math.random() * (i + 1)); const randomIndex = Math.floor(Math.random() * (i + 1));
const tmp = array[randomIndex]; [array[i], array[randomIndex]] = [array[randomIndex], array[i]];
array[randomIndex] = array[i];
array[i] = tmp;
} }
return array; return array;