Skip to content

Commit ad35e3b

Browse files
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Miscellaneous: Flipping bits.
* TEST data moved to JSON. * Adjusted the interface to match what hackerrank expects.
1 parent f2b980d commit ad35e3b

File tree

3 files changed

+57
-51
lines changed

3 files changed

+57
-51
lines changed

src/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
const __BINARY_BASE__ = 2;
66
const __NUMBER_SIZE_IN_BITS__ = 32;
77

8-
export function flippingBits(n) {
8+
function flippingBits(n) {
99
let nBinaryStr = n.toString(__BINARY_BASE__);
1010
nBinaryStr = nBinaryStr.padStart(__NUMBER_SIZE_IN_BITS__, '0');
1111

@@ -23,3 +23,4 @@ export function flippingBits(n) {
2323
}
2424

2525
export default { flippingBits };
26+
export { flippingBits };

src/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.test.js

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,7 @@ import { describe, expect, it } from '@jest/globals';
22
import { logger as console } from '../../../logger.js';
33

44
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-
];
5+
import TEST_CASES from './flipping-bits.testcases.json';
556

567
describe('flipping bits', () => {
578
it('flipping bits test cases', () => {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
]

0 commit comments

Comments
 (0)