Procházet zdrojové kódy

Add new execise hexadecimal

Matthew Morgan před 10 roky
rodič
revize
218fca55aa

+ 1
- 0
config.json Zobrazit soubor

@@ -31,6 +31,7 @@
31 31
     "crypto-square",
32 32
     "trinary",
33 33
     "rna-transcription",
34
+    "hexadecimal",
34 35
     "sieve",
35 36
     "simple-cipher",
36 37
     "octal",

+ 11
- 0
hexadecimal/build.gradle Zobrazit soubor

@@ -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
+}

+ 15
- 0
hexadecimal/src/example/java/Hexadecimal.java Zobrazit soubor

@@ -0,0 +1,15 @@
1
+import java.util.Arrays;
2
+
3
+public class Hexadecimal {
4
+    private static final String HEXES = "0123456789abcdef";
5
+    public static int toDecimal(String input){
6
+        boolean  isHex = input.matches("^[0-9a-f]+");
7
+        if (!isHex) return 0;
8
+        return Arrays.stream(input.split(""))
9
+                .map(HEXES::indexOf)
10
+                .reduce(0, (decimal, hex) -> decimal * 16 + hex);
11
+
12
+    }
13
+}
14
+
15
+

+ 0
- 0
hexadecimal/src/main/java/.keep Zobrazit soubor


+ 0
- 0
hexadecimal/src/test/java/.keep Zobrazit soubor


+ 65
- 0
hexadecimal/src/test/java/HexadecimalTest.java Zobrazit soubor

@@ -0,0 +1,65 @@
1
+import org.junit.Test;
2
+import static org.junit.Assert.assertEquals;
3
+
4
+public class HexadecimalTest {
5
+
6
+    @Test
7
+    public void testOne(){
8
+        int expected = 1;
9
+        assertEquals(expected, Hexadecimal.toDecimal("1"));
10
+    }
11
+
12
+    @Test
13
+    public void testC(){
14
+        int expected = 12;
15
+        assertEquals(expected, Hexadecimal.toDecimal("c"));
16
+    }
17
+
18
+    @Test
19
+    public void test10(){
20
+        int expected = 16;
21
+        assertEquals(expected, Hexadecimal.toDecimal("10"));
22
+    }
23
+
24
+    @Test
25
+    public void testAf(){
26
+        int expected = 175;
27
+        assertEquals(expected, Hexadecimal.toDecimal("af"));
28
+    }
29
+
30
+    @Test
31
+    public void test100(){
32
+        int expected = 256;
33
+        assertEquals(expected, Hexadecimal.toDecimal("100"));
34
+    }
35
+
36
+    @Test
37
+    public void test19ace(){
38
+        int expected = 105166;
39
+        assertEquals(expected, Hexadecimal.toDecimal("19ace"));
40
+    }
41
+
42
+    @Test
43
+    public void testInvalid(){
44
+        int expected = 0;
45
+        assertEquals(expected, Hexadecimal.toDecimal("carrot"));
46
+    }
47
+
48
+    @Test
49
+    public void testBlack(){
50
+        int expected = 0;
51
+        assertEquals(expected, Hexadecimal.toDecimal("000000"));
52
+    }
53
+
54
+    @Test
55
+    public void testWhite(){
56
+        int expected = 16777215;
57
+        assertEquals(expected, Hexadecimal.toDecimal("ffffff"));
58
+    }
59
+
60
+    @Test
61
+    public void testYellow(){
62
+        int expected = 16776960;
63
+        assertEquals(expected, Hexadecimal.toDecimal("ffff00"));
64
+    }
65
+}

+ 1
- 0
settings.gradle Zobrazit soubor

@@ -9,6 +9,7 @@ include 'etl'
9 9
 include 'gigasecond'
10 10
 include 'grade-school'
11 11
 include 'hamming'
12
+include 'hexadecimal'
12 13
 include 'hello-world'
13 14
 include 'luhn'
14 15
 include 'meetup'