Skip to content

Commit e36c441

Browse files
Bruce-Feldmantrekhleb
authored andcommitted
Minor fixes. (trekhleb#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.
1 parent 93bfe97 commit e36c441

File tree

10 files changed

+15
-75
lines changed

10 files changed

+15
-75
lines changed

src/algorithms/math/bits/README.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
#### Get Bit
44

5-
This method shifts `1` over by `bitPosition` bits, creating a
6-
value that looks like `00100`. Then we perform `AND` operation
7-
that clears all bits from the original number except the
8-
`bitPosition` one. Then we compare the result with zero. If
9-
result is zero that would mean that original number has `0` at
10-
position `bitPosition`.
5+
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'.
116

127
> See `getBit` function for further details.
138

src/algorithms/math/bits/getBit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* @return {number}
55
*/
66
export default function getBit(number, bitPosition) {
7-
return (number & (1 << bitPosition)) === 0 ? 0 : 1;
7+
return (number >> bitPosition) & 1;
88
}

src/algorithms/math/euclidean-algorithm/__test__/euclieanAlgorithm.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import euclideanAlgorithm from '../euclideanAlgorithm';
22

33
describe('euclideanAlgorithm', () => {
44
it('should calculate GCD', () => {
5-
expect(euclideanAlgorithm(0, 0)).toBeNull();
5+
expect(euclideanAlgorithm(0, 0)).toBe(0);
66
expect(euclideanAlgorithm(2, 0)).toBe(2);
77
expect(euclideanAlgorithm(0, 2)).toBe(2);
88
expect(euclideanAlgorithm(1, 2)).toBe(1);
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
/**
22
* @param {number} originalA
33
* @param {number} originalB
4-
* @return {number|null}
4+
* @return {number}
55
*/
66
export default function euclideanAlgorithm(originalA, originalB) {
77
const a = Math.abs(originalA);
88
const b = Math.abs(originalB);
99

10-
if (a === 0 && b === 0) {
11-
return null;
12-
}
13-
14-
if (a === 0 && b !== 0) {
15-
return b;
16-
}
17-
18-
if (a !== 0 && b === 0) {
19-
return a;
20-
}
21-
22-
// Normally we need to do subtraction (a - b) but to prevent
23-
// recursion occurs to often we may shorten subtraction to (a % b).
24-
// Since (a % b) is normally means that we've subtracted b from a
25-
// many times until the difference became less then a.
26-
27-
if (a > b) {
28-
return euclideanAlgorithm(a % b, b);
29-
}
30-
31-
return euclideanAlgorithm(b % a, a);
10+
return (b === 0) ? a : euclideanAlgorithm(b, a % b);
3211
}

src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import isPowerOfTwo from '../isPowerOfTwo';
22

33
describe('isPowerOfTwo', () => {
4-
it('should throw an exception when trying to apply function to negative number', () => {
5-
const isNegativePowerOfTwo = () => {
6-
isPowerOfTwo(-1);
7-
};
8-
9-
expect(isNegativePowerOfTwo).toThrowError();
10-
});
11-
124
it('should check if the number is made by multiplying twos', () => {
5+
expect(isPowerOfTwo(-1)).toBeFalsy();
136
expect(isPowerOfTwo(0)).toBeFalsy();
14-
expect(isPowerOfTwo(1)).toBeFalsy();
7+
expect(isPowerOfTwo(1)).toBeTruthy();
158
expect(isPowerOfTwo(2)).toBeTruthy();
169
expect(isPowerOfTwo(3)).toBeFalsy();
1710
expect(isPowerOfTwo(4)).toBeTruthy();

src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import isPowerOfTwoBitwise from '../isPowerOfTwoBitwise';
22

33
describe('isPowerOfTwoBitwise', () => {
4-
it('should throw an exception when trying to apply function to negative number', () => {
5-
const isNegativePowerOfTwo = () => {
6-
isPowerOfTwoBitwise(-1);
7-
};
8-
9-
expect(isNegativePowerOfTwo).toThrowError();
10-
});
11-
124
it('should check if the number is made by multiplying twos', () => {
5+
expect(isPowerOfTwoBitwise(-1)).toBeFalsy();
136
expect(isPowerOfTwoBitwise(0)).toBeFalsy();
14-
expect(isPowerOfTwoBitwise(1)).toBeFalsy();
7+
expect(isPowerOfTwoBitwise(1)).toBeTruthy();
158
expect(isPowerOfTwoBitwise(2)).toBeTruthy();
169
expect(isPowerOfTwoBitwise(3)).toBeFalsy();
1710
expect(isPowerOfTwoBitwise(4)).toBeTruthy();

src/algorithms/math/is-power-of-two/isPowerOfTwo.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
* @return {boolean}
44
*/
55
export default function isPowerOfTwo(number) {
6-
// Don't work with negative numbers.
7-
if (number < 0) {
8-
throw new Error('Please provide positive number');
9-
}
10-
11-
// 0 and 1 are not powers of two.
12-
if (number <= 1) {
6+
// 1 (2^0) is the smallest power of two.
7+
if (number < 1) {
138
return false;
149
}
1510

src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
* @return {boolean}
44
*/
55
export default function isPowerOfTwoBitwise(number) {
6-
// Don't work with negative numbers.
7-
if (number < 0) {
8-
throw new Error('Please provide positive number');
9-
}
10-
11-
// 0 and 1 are not powers of two.
12-
if (number <= 1) {
6+
// 1 (2^0) is the smallest power of two.
7+
if (number < 1) {
138
return false;
149
}
1510

src/algorithms/math/least-common-multiple/leastCommonMultiple.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,5 @@ import euclideanAlgorithm from '../euclidean-algorithm/euclideanAlgorithm';
77
*/
88

99
export default function leastCommonMultiple(a, b) {
10-
if (a === 0 && b === 0) {
11-
return 0;
12-
}
13-
14-
return Math.abs(a * b) / euclideanAlgorithm(a, b);
10+
return ((a === 0) || (b === 0)) ? 0 : Math.abs(a * b) / euclideanAlgorithm(a, b);
1511
}

src/algorithms/sets/fisher-yates/fisherYates.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@ export default function fisherYates(originalArray) {
66
// Clone array from preventing original array from modification (for testing purpose).
77
const array = originalArray.slice(0);
88

9-
if (array.length <= 1) {
10-
return array;
11-
}
12-
139
for (let i = (array.length - 1); i > 0; i -= 1) {
1410
const randomIndex = Math.floor(Math.random() * (i + 1));
15-
const tmp = array[randomIndex];
16-
array[randomIndex] = array[i];
17-
array[i] = tmp;
11+
[array[i], array[randomIndex]] = [array[randomIndex], array[i]];
1812
}
1913

2014
return array;

0 commit comments

Comments
 (0)