Skip to content

Commit cb10cd2

Browse files
author
Piet van Dongen
committed
Added exercise binary
1 parent 7407b1e commit cb10cd2

6 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎binary/build.gradle‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.10"
11+
}

‎binary/example.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class Binary {
2+
3+
private String binary;
4+
private int decimal;
5+
6+
public Binary(String binary) {
7+
this.binary = binary;
8+
this.decimal = getDecimalFromBinary(binary);
9+
}
10+
11+
public int getDecimal() {
12+
return decimal;
13+
}
14+
15+
private static int getDecimalFromBinary(String binary) {
16+
if (!isValid(binary)) {
17+
return 0;
18+
}
19+
20+
int sum = 0;
21+
22+
for (int i = 0; i < binary.length(); i++) {
23+
sum += Character.getNumericValue(binary.charAt(i)) * (int) Math.pow(2, binary.length() - i - 1);
24+
}
25+
26+
return sum;
27+
}
28+
29+
private static boolean isValid(String binaryRepresentation) {
30+
boolean isValid = binaryRepresentation.chars()
31+
.allMatch(x -> Character.isDigit((char) x) && Character.getNumericValue((char) x) < 2);
32+
33+
return isValid;
34+
}
35+
}

‎binary/src/main/java/.keep‎

Whitespace-only changes.

‎binary/src/main/java/Binary.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public class Binary {
2+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import org.junit.Test;
2+
import org.junit.runner.RunWith;
3+
import org.junit.runners.Parameterized;
4+
import org.junit.runners.Parameterized.Parameters;
5+
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
@RunWith(Parameterized.class)
12+
public class BinaryTest {
13+
14+
private String input;
15+
private int expectedOutput;
16+
17+
@Parameters
18+
public static Collection<Object[]> data() {
19+
return Arrays.asList(new Object[][]{
20+
{"1", 1},
21+
{"10", 2},
22+
{"11", 3},
23+
{"100", 4},
24+
{"1001", 9},
25+
{"11010", 26},
26+
{"10001101000", 1128},
27+
{"2", 0},
28+
{"5", 0},
29+
{"9", 0},
30+
{"134678", 0},
31+
{"abc10z", 0},
32+
{"011", 3}
33+
});
34+
}
35+
36+
public BinaryTest(String input, int expectedOutput) {
37+
this.input = input;
38+
this.expectedOutput = expectedOutput;
39+
}
40+
41+
@Test
42+
public void test() {
43+
Binary binary = new Binary(input);
44+
45+
assertEquals(expectedOutput, binary.getDecimal());
46+
}
47+
}

‎config.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"gigasecond",
1919
"triangle",
2020
"scrabble-score",
21-
"roman-numerals"
21+
"roman-numerals",
22+
"binary"
2223
],
2324
"deprecated": [
2425
],

0 commit comments

Comments
 (0)