Skip to content

Commit 601ce70

Browse files
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Miscellaneous: Flipping bits.
* Adjusted the interface to match what hackerrank expects.
1 parent 9ef141a commit 601ce70

File tree

3 files changed

+59
-54
lines changed

3 files changed

+59
-54
lines changed

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

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

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

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,16 @@
11
import { describe, expect, it } from '@jest/globals';
22
import { logger as console } from '../../../logger.js';
33

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';
556

567
describe('flipping bits', () => {
578
it('flipping bits test cases', () => {
589
expect.assertions(8);
5910

6011
TEST_CASES.forEach((testSet) => {
6112
testSet.tests.forEach((test) => {
62-
const answer = flippingBits(test.input);
13+
const answer = flippingBits.flippingBits(test.input);
6314

6415
console.debug(`flippingBits(${test.input}) solution found: ${answer}`);
6516

@@ -74,7 +25,7 @@ describe('flipping bits', () => {
7425
const input = 9;
7526
const expected = 4294967286;
7627

77-
const answer = flippingBits(input);
28+
const answer = flippingBits.flippingBits(input);
7829

7930
console.debug(`flippingBits(${input}) solution found: ${answer}`);
8031

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)