Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite36c441

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 parent93bfe97 commite36c441

File tree

10 files changed

+15
-75
lines changed

10 files changed

+15
-75
lines changed

‎src/algorithms/math/bits/README.md

Lines changed: 1 addition & 6 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
*@return {number}
55
*/
66
exportdefaultfunctiongetBit(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

Lines changed: 1 addition & 1 deletion
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);
Lines changed: 2 additions & 23 deletions
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
exportdefaultfunctioneuclideanAlgorithm(originalA,originalB){
77
consta=Math.abs(originalA);
88
constb=Math.abs(originalB);
99

10-
if(a===0&&b===0){
11-
returnnull;
12-
}
13-
14-
if(a===0&&b!==0){
15-
returnb;
16-
}
17-
18-
if(a!==0&&b===0){
19-
returna;
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-
returneuclideanAlgorithm(a%b,b);
29-
}
30-
31-
returneuclideanAlgorithm(b%a,a);
10+
return(b===0) ?a :euclideanAlgorithm(b,a%b);
3211
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
importisPowerOfTwofrom'../isPowerOfTwo';
22

33
describe('isPowerOfTwo',()=>{
4-
it('should throw an exception when trying to apply function to negative number',()=>{
5-
constisNegativePowerOfTwo=()=>{
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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
importisPowerOfTwoBitwisefrom'../isPowerOfTwoBitwise';
22

33
describe('isPowerOfTwoBitwise',()=>{
4-
it('should throw an exception when trying to apply function to negative number',()=>{
5-
constisNegativePowerOfTwo=()=>{
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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
*@return {boolean}
44
*/
55
exportdefaultfunctionisPowerOfTwo(number){
6-
// Don't work with negative numbers.
7-
if(number<0){
8-
thrownewError('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
returnfalse;
149
}
1510

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
*@return {boolean}
44
*/
55
exportdefaultfunctionisPowerOfTwoBitwise(number){
6-
// Don't work with negative numbers.
7-
if(number<0){
8-
thrownewError('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
returnfalse;
149
}
1510

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

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

99
exportdefaultfunctionleastCommonMultiple(a,b){
10-
if(a===0&&b===0){
11-
return0;
12-
}
13-
14-
returnMath.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

Lines changed: 1 addition & 7 deletions
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
constarray=originalArray.slice(0);
88

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

2014
returnarray;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp