Skip to content

Commit 521a936

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Flipping bits. New way to solve ✅.
1 parent d191b41 commit 521a936

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ae.hackerrank.interview_preparation_kit.miscellaneous;
2+
3+
/**
4+
* AngryFlorist.
5+
*
6+
* @link Problem definition
7+
* [[docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md]]
8+
*/
9+
public class FlippingBitsAlternative {
10+
11+
private FlippingBitsAlternative() {}
12+
13+
/**
14+
* flippingBits.
15+
*/
16+
public static long flippingBits(long n) {
17+
return ~n & 0xFFFFFFFFL; // Use bitwise NOT and mask to ensure 32-bit unsigned result
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package ae.hackerrank.interview_preparation_kit.miscellaneous;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
import util.JsonLoader;
12+
13+
/**
14+
* FlippingBitsTflippingBits.
15+
*/
16+
@TestInstance(Lifecycle.PER_CLASS)
17+
class FlippingBitsTestAlternativeTest {
18+
/**
19+
* FlippingBitsTestAlternativeTestCaseTest.
20+
*/
21+
public static class FlippingBitsTestAlternativeTestCaseTest {
22+
public long input;
23+
public long answer;
24+
}
25+
26+
/**
27+
* FlippingBitsTestAlternativeTestCase.
28+
*/
29+
public static class FlippingBitsTestAlternativeTestCase {
30+
public String title;
31+
public List<FlippingBitsTestAlternativeTestCaseTest> tests;
32+
}
33+
34+
private List<FlippingBitsTestAlternativeTestCase> testCases;
35+
36+
@BeforeAll
37+
void setup() throws IOException {
38+
String path = String.join("/",
39+
"hackerrank",
40+
"interview_preparation_kit",
41+
"miscellaneous",
42+
"flipping_bits.testcases.json");
43+
44+
this.testCases = JsonLoader.loadJson(path, FlippingBitsTestAlternativeTestCase.class);
45+
}
46+
47+
@Test
48+
void testLuckBalance() {
49+
for (FlippingBitsTestAlternativeTestCase tests : testCases) {
50+
for (FlippingBitsTestAlternativeTestCaseTest test : tests.tests) {
51+
Long result = FlippingBitsAlternative.flippingBits(test.input);
52+
53+
assertEquals(test.answer, result,
54+
"%s(%s) => must be: %d".formatted(
55+
"FlippingBits.flippingBits",
56+
test.input,
57+
test.answer));
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy