File tree 3 files changed +59
-54
lines changed
src/hackerrank/interview_preparation_kit/miscellaneous
3 files changed +59
-54
lines changed Original file line number Diff line number Diff line change 5
5
const __BINARY_BASE__ = 2 ;
6
6
const __NUMBER_SIZE_IN_BITS__ = 32 ;
7
7
8
- export function flippingBits ( n ) {
8
+ function flippingBits ( n ) {
9
9
let nBinaryStr = n . toString ( __BINARY_BASE__ ) ;
10
10
nBinaryStr = nBinaryStr . padStart ( __NUMBER_SIZE_IN_BITS__ , '0' ) ;
11
11
Original file line number Diff line number Diff line change 1
1
import { describe , expect , it } from '@jest/globals' ;
2
2
import { logger as console } from '../../../logger.js' ;
3
3
4
- import { flippingBits } from './flipping-bits.js' ;
5
-
6
- const TEST_CASES = [
7
- {
8
- title : 'Sample Test Case 0' ,
9
- tests : [
10
- {
11
- input : 2147483647 ,
12
- expected : 2147483648
13
- } ,
14
- {
15
- input : 1 ,
16
- expected : 4294967294
17
- } ,
18
- {
19
- input : 0 ,
20
- expected : 4294967295
21
- }
22
- ]
23
- } ,
24
- {
25
- title : 'Sample Test Case 1' ,
26
- tests : [
27
- {
28
- input : 4 ,
29
- expected : 4294967291
30
- } ,
31
- {
32
- input : 123456 ,
33
- expected : 4294843839
34
- }
35
- ]
36
- } ,
37
- {
38
- title : 'Sample Test Case 2' ,
39
- tests : [
40
- {
41
- input : 0 ,
42
- expected : 4294967295
43
- } ,
44
- {
45
- input : 802743475 ,
46
- expected : 3492223820
47
- } ,
48
- {
49
- input : 35601423 ,
50
- expected : 4259365872
51
- }
52
- ]
53
- }
54
- ] ;
4
+ import flippingBits from './flipping-bits.js' ;
5
+ import TEST_CASES from './flipping-bits.testcases.json' ;
55
6
56
7
describe ( 'flipping bits' , ( ) => {
57
8
it ( 'flipping bits test cases' , ( ) => {
58
9
expect . assertions ( 8 ) ;
59
10
60
11
TEST_CASES . forEach ( ( testSet ) => {
61
12
testSet . tests . forEach ( ( test ) => {
62
- const answer = flippingBits ( test . input ) ;
13
+ const answer = flippingBits . flippingBits ( test . input ) ;
63
14
64
15
console . debug ( `flippingBits(${ test . input } ) solution found: ${ answer } ` ) ;
65
16
@@ -74,7 +25,7 @@ describe('flipping bits', () => {
74
25
const input = 9 ;
75
26
const expected = 4294967286 ;
76
27
77
- const answer = flippingBits ( input ) ;
28
+ const answer = flippingBits . flippingBits ( input ) ;
78
29
79
30
console . debug ( `flippingBits(${ input } ) solution found: ${ answer } ` ) ;
80
31
Original file line number Diff line number Diff line change
1
+ [
2
+ {
3
+ "title" : " Sample Test Case 0" ,
4
+ "tests" :
5
+ [
6
+ {
7
+ "input" : 2147483647 ,
8
+ "expected" : 2147483648
9
+ },
10
+ {
11
+ "input" : 1 ,
12
+ "expected" : 4294967294
13
+ },
14
+ {
15
+ "input" : 0 ,
16
+ "expected" : 4294967295
17
+ }
18
+ ]
19
+ },
20
+ {
21
+ "title" : " Sample Test Case 1" ,
22
+ "tests" :
23
+ [
24
+
25
+ {
26
+ "input" : 4 ,
27
+ "expected" : 4294967291
28
+ },
29
+ {
30
+ "input" : 123456 ,
31
+ "expected" : 4294843839
32
+ }
33
+ ]
34
+ },
35
+ {
36
+ "title" : " Sample Test Case 2" ,
37
+ "tests" :
38
+ [
39
+
40
+ {
41
+ "input" : 0 ,
42
+ "expected" : 4294967295
43
+ },
44
+ {
45
+ "input" : 802743475 ,
46
+ "expected" : 3492223820
47
+ },
48
+ {
49
+ "input" : 35601423 ,
50
+ "expected" : 4259365872
51
+ }
52
+ ]
53
+ }
54
+ ]
You can’t perform that action at this time.
0 commit comments