Przeglądaj źródła

secret-handshake: add to track (#257)

Stuart Kent 9 lat temu
rodzic
commit
b43bf8ff8f

+ 7
- 1
config.json Wyświetl plik

@@ -58,7 +58,8 @@
58 58
     "sublist",
59 59
     "rectangles",
60 60
     "matrix",
61
-    "diamond"
61
+    "diamond",
62
+    "secret-handshake"
62 63
   ],
63 64
   "exercises": [
64 65
     {
@@ -335,6 +336,11 @@
335 336
       "slug": "diamond",
336 337
       "difficulty": 1,
337 338
       "topics": []
339
+    },
340
+    {
341
+      "slug": "secret-handshake",
342
+      "difficulty": 1,
343
+      "topics": []
338 344
     }
339 345
   ],
340 346
   "deprecated": [

+ 17
- 0
exercises/secret-handshake/build.gradle Wyświetl plik

@@ -0,0 +1,17 @@
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.12"
11
+}
12
+test {
13
+  testLogging {
14
+    exceptionFormat = 'full'
15
+    events = ["passed", "failed", "skipped"]
16
+  }
17
+}

+ 29
- 0
exercises/secret-handshake/src/example/java/HandshakeCalculator.java Wyświetl plik

@@ -0,0 +1,29 @@
1
+import java.util.ArrayList;
2
+import java.util.Collections;
3
+import java.util.List;
4
+
5
+final class HandshakeCalculator {
6
+
7
+    private static final int REVERSE_SIGNALS_BIT_POSITION = 4;
8
+
9
+    List<Signal> calculateHandshake(final int number) {
10
+        final List<Signal> result = new ArrayList<>();
11
+
12
+        for (final Signal signal : Signal.values()) {
13
+            if (isBitSet(signal.ordinal(), number)) {
14
+                result.add(signal);
15
+            }
16
+        }
17
+
18
+        if (isBitSet(REVERSE_SIGNALS_BIT_POSITION, number)) {
19
+            Collections.reverse(result);
20
+        }
21
+
22
+        return result;
23
+    }
24
+
25
+    private boolean isBitSet(final int position, final int number) {
26
+        return ((number >> position) & 1) == 1;
27
+    }
28
+
29
+}

+ 1
- 0
exercises/secret-handshake/src/example/java/Signal.java Wyświetl plik

@@ -0,0 +1 @@
1
+../../main/java/Signal.java

+ 5
- 0
exercises/secret-handshake/src/main/java/HandshakeCalculator.java Wyświetl plik

@@ -0,0 +1,5 @@
1
+final class HandshakeCalculator {
2
+
3
+
4
+
5
+}

+ 5
- 0
exercises/secret-handshake/src/main/java/Signal.java Wyświetl plik

@@ -0,0 +1,5 @@
1
+enum Signal {
2
+
3
+    WINK, DOUBLE_BLINK, CLOSE_YOUR_EYES, JUMP
4
+
5
+}

+ 106
- 0
exercises/secret-handshake/src/test/java/HandshakeCalculatorTest.java Wyświetl plik

@@ -0,0 +1,106 @@
1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import static java.util.Arrays.asList;
5
+import static java.util.Collections.emptyList;
6
+import static java.util.Collections.singletonList;
7
+import static org.junit.Assert.assertEquals;
8
+
9
+public final class HandshakeCalculatorTest {
10
+
11
+    @Test
12
+    public void testThatInput1YieldsAWink() {
13
+        assertEquals(
14
+                singletonList(Signal.WINK),
15
+                new HandshakeCalculator().calculateHandshake(1));
16
+    }
17
+
18
+    @Ignore
19
+    @Test
20
+    public void testThatInput2YieldsADoubleBlink() {
21
+        assertEquals(
22
+                singletonList(Signal.DOUBLE_BLINK),
23
+                new HandshakeCalculator().calculateHandshake(2));
24
+    }
25
+
26
+    @Ignore
27
+    @Test
28
+    public void testThatInput4YieldsACloseYourEyes() {
29
+        assertEquals(
30
+                singletonList(Signal.CLOSE_YOUR_EYES),
31
+                new HandshakeCalculator().calculateHandshake(4));
32
+    }
33
+
34
+    @Ignore
35
+    @Test
36
+    public void testThatInput8YieldsAJump() {
37
+        assertEquals(
38
+                singletonList(Signal.JUMP),
39
+                new HandshakeCalculator().calculateHandshake(8));
40
+    }
41
+
42
+    @Ignore
43
+    @Test
44
+    public void testAnInputThatYieldsTwoActions() {
45
+        assertEquals(
46
+                asList(Signal.WINK, Signal.DOUBLE_BLINK),
47
+                new HandshakeCalculator().calculateHandshake(3));
48
+    }
49
+
50
+    @Ignore
51
+    @Test
52
+    public void testAnInputThatYieldsTwoReversedActions() {
53
+        assertEquals(
54
+                asList(Signal.DOUBLE_BLINK, Signal.WINK),
55
+                new HandshakeCalculator().calculateHandshake(19));
56
+    }
57
+
58
+    @Ignore
59
+    @Test
60
+    public void testReversingASingleActionYieldsTheSameAction() {
61
+        assertEquals(
62
+                singletonList(Signal.JUMP),
63
+                new HandshakeCalculator().calculateHandshake(24));
64
+    }
65
+
66
+    @Ignore
67
+    @Test
68
+    public void testReversingNoActionsYieldsNoActions() {
69
+        assertEquals(
70
+                emptyList(),
71
+                new HandshakeCalculator().calculateHandshake(16));
72
+    }
73
+
74
+    @Ignore
75
+    @Test
76
+    public void testInputThatYieldsAllActions() {
77
+        assertEquals(
78
+                asList(Signal.WINK, Signal.DOUBLE_BLINK, Signal.CLOSE_YOUR_EYES, Signal.JUMP),
79
+                new HandshakeCalculator().calculateHandshake(15));
80
+    }
81
+
82
+    @Ignore
83
+    @Test
84
+    public void testInputThatYieldsAllActionsReversed() {
85
+        assertEquals(
86
+                asList(Signal.JUMP, Signal.CLOSE_YOUR_EYES, Signal.DOUBLE_BLINK, Signal.WINK),
87
+                new HandshakeCalculator().calculateHandshake(31));
88
+    }
89
+
90
+    @Ignore
91
+    @Test
92
+    public void testThatInput0YieldsNoActions() {
93
+        assertEquals(
94
+                emptyList(),
95
+                new HandshakeCalculator().calculateHandshake(0));
96
+    }
97
+
98
+    @Ignore
99
+    @Test
100
+    public void testThatInputWithLower5BitsNotSetYieldsNoActions() {
101
+        assertEquals(
102
+                emptyList(),
103
+                new HandshakeCalculator().calculateHandshake(32));
104
+    }
105
+
106
+}

+ 1
- 0
exercises/settings.gradle Wyświetl plik

@@ -44,6 +44,7 @@ include 'robot-name'
44 44
 include 'robot-simulator'
45 45
 include 'roman-numerals'
46 46
 include 'scrabble-score'
47
+include 'secret-handshake'
47 48
 include 'series'
48 49
 include 'sieve'
49 50
 include 'simple-cipher'