Piet van Dongen 11 лет назад
Родитель
Сommit
602f3895df

+ 2
- 1
config.json Просмотреть файл

@@ -31,7 +31,8 @@
31 31
     "rna-transcription",
32 32
     "sieve",
33 33
     "simple-cipher",
34
-    "pascals-triangle"	
34
+    "octal",
35
+    "pascals-triangle"
35 36
   ],
36 37
   "deprecated": [
37 38
   ],

+ 11
- 0
octal/build.gradle Просмотреть файл

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

+ 33
- 0
octal/example.java Просмотреть файл

@@ -0,0 +1,33 @@
1
+public class Octal {
2
+
3
+    private String octal;
4
+    private int decimal;
5
+
6
+    public Octal(String octal) {
7
+        this.octal = octal;
8
+        this.decimal = getDecimalFromOctal(octal);
9
+    }
10
+
11
+    public int getDecimal() {
12
+        return decimal;
13
+    }
14
+
15
+    private static int getDecimalFromOctal(String octal) {
16
+        if (!isValid(octal)) {
17
+            return 0;
18
+        }
19
+
20
+        int sum = 0;
21
+
22
+        for (int index = 0; index < octal.length(); index++) {
23
+            sum += Character.getNumericValue(octal.charAt(index)) * Math.pow(8, octal.length() - index - 1);
24
+        }
25
+
26
+        return sum;
27
+    }
28
+
29
+    private static boolean isValid(String binaryRepresentation) {
30
+        return binaryRepresentation.chars()
31
+                .allMatch(x -> Character.isDigit((char) x) && Character.getNumericValue((char) x) < 8);
32
+    }
33
+}

+ 0
- 0
octal/src/main/java/.keep Просмотреть файл


+ 2
- 0
octal/src/main/java/Octal.java Просмотреть файл

@@ -0,0 +1,2 @@
1
+public class Octal {
2
+}

+ 47
- 0
octal/src/test/java/OctalTest.java Просмотреть файл

@@ -0,0 +1,47 @@
1
+import org.junit.Test;
2
+import org.junit.runner.RunWith;
3
+import org.junit.runners.Parameterized;
4
+
5
+import java.util.Arrays;
6
+import java.util.Collection;
7
+
8
+import static org.junit.Assert.assertEquals;
9
+
10
+@RunWith(Parameterized.class)
11
+public class OctalTest {
12
+
13
+    private String input;
14
+    private int expectedOutput;
15
+
16
+    @Parameterized.Parameters
17
+    public static Collection<Object[]> data() {
18
+        return Arrays.asList(new Object[][]{
19
+                {"1", 1},
20
+                {"10", 8},
21
+                {"17", 15},
22
+                {"11", 9},
23
+                {"130", 88},
24
+                {"2047", 1063},
25
+                {"7777", 4095},
26
+                {"1234567", 342391},
27
+                {"carrot", 0},
28
+                {"8", 0},
29
+                {"9", 0},
30
+                {"6789", 0},
31
+                {"abc1z", 0},
32
+                {"011", 9}
33
+        });
34
+    }
35
+
36
+    public OctalTest(String input, int expectedOutput) {
37
+        this.input = input;
38
+        this.expectedOutput = expectedOutput;
39
+    }
40
+
41
+    @Test
42
+    public void test() {
43
+        Octal octal = new Octal(input);
44
+
45
+        assertEquals(expectedOutput, octal.getDecimal());
46
+    }
47
+}