瀏覽代碼

Added scrabble-score exercise

Piet van Dongen 11 年之前
父節點
當前提交
62c4c39b5d

+ 2
- 1
config.json 查看文件

@@ -16,7 +16,8 @@
16 16
     "grade-school",
17 17
     "space-age",
18 18
     "gigasecond",
19
-    "triangle"
19
+    "triangle",
20
+    "scrabble-score"
20 21
   ],
21 22
   "deprecated": [
22 23
   ],

+ 11
- 0
scrabble-score/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
+}

+ 59
- 0
scrabble-score/example.java 查看文件

@@ -0,0 +1,59 @@
1
+public class Scrabble {
2
+
3
+    private String word;
4
+
5
+    private static final Map<Character, Integer> letterScores = new HashMap<>();
6
+
7
+    static {
8
+        letterScores.put('a', 1);
9
+        letterScores.put('e', 1);
10
+        letterScores.put('i', 1);
11
+        letterScores.put('o', 1);
12
+        letterScores.put('u', 1);
13
+        letterScores.put('l', 1);
14
+        letterScores.put('n', 1);
15
+        letterScores.put('r', 1);
16
+        letterScores.put('s', 1);
17
+        letterScores.put('t', 1);
18
+        letterScores.put('d', 2);
19
+        letterScores.put('g', 2);
20
+        letterScores.put('b', 3);
21
+        letterScores.put('c', 3);
22
+        letterScores.put('m', 3);
23
+        letterScores.put('p', 3);
24
+        letterScores.put('f', 4);
25
+        letterScores.put('h', 4);
26
+        letterScores.put('v', 4);
27
+        letterScores.put('w', 4);
28
+        letterScores.put('y', 4);
29
+        letterScores.put('k', 5);
30
+        letterScores.put('j', 8);
31
+        letterScores.put('x', 8);
32
+        letterScores.put('q', 10);
33
+        letterScores.put('z', 10);
34
+    }
35
+
36
+    private Object score;
37
+
38
+    public Scrabble(String word) {
39
+        this.word = word;
40
+    }
41
+
42
+    public int getScore() {
43
+        return getScore(word);
44
+    }
45
+
46
+    public static int getScore(String input) {
47
+        if (input == null || input.trim().isEmpty()) {
48
+            return 0;
49
+        }
50
+
51
+        int score = 0;
52
+
53
+        for (char letter : input.toLowerCase().toCharArray()) {
54
+            score += letterScores.get(letter);
55
+        }
56
+
57
+        return score;
58
+    }
59
+}

+ 0
- 0
scrabble-score/src/main/java/.keep 查看文件


+ 2
- 0
scrabble-score/src/main/java/Scrabble.java 查看文件

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

+ 67
- 0
scrabble-score/src/test/java/ScrabbleScoreTest.java 查看文件

@@ -0,0 +1,67 @@
1
+import org.junit.Test;
2
+
3
+public class ScrabbleScoreTest {
4
+
5
+    @Test
6
+    public void emptyWordScoresZero() {
7
+        Scrabble scrabble = new Scrabble("");
8
+
9
+        assertEquals(scrabble.getScore(), 0);
10
+    }
11
+
12
+    @Test
13
+    public void whitespaceWordScoresZero() {
14
+        Scrabble scrabble = new Scrabble(" \t\n");
15
+
16
+        assertEquals(scrabble.getScore(), 0);
17
+    }
18
+
19
+    @Test
20
+    public void nullScoresZero() {
21
+        Scrabble scrabble = new Scrabble(null);
22
+
23
+        assertEquals(scrabble.getScore(), 0);
24
+    }
25
+
26
+    @Test
27
+    public void scoresVeryShortWord() {
28
+        Scrabble scrabble = new Scrabble("a");
29
+
30
+        assertEquals(scrabble.getScore(), 1);
31
+    }
32
+
33
+    @Test
34
+    public void scoresAnotherVeryShortWord() {
35
+        Scrabble scrabble = new Scrabble("f");
36
+
37
+        assertEquals(scrabble.getScore(), 4);
38
+    }
39
+
40
+    @Test
41
+    public void simpleWordScoresTheNumberOfLetters() {
42
+        Scrabble scrabble = new Scrabble("street");
43
+
44
+        assertEquals(scrabble.getScore(), 6);
45
+    }
46
+
47
+    @Test
48
+    public void complicatedWordScoresMore() {
49
+        Scrabble scrabble = new Scrabble("quirky");
50
+
51
+        assertEquals(scrabble.getScore(), 22);
52
+    }
53
+
54
+    @Test
55
+    public void scoresAreCaseInsensitive() {
56
+        Scrabble scrabble = new Scrabble("MULTIBILLIONAIRE");
57
+
58
+        assertEquals(scrabble.getScore(), 20);
59
+    }
60
+
61
+    @Test
62
+    public void convenientScoring() {
63
+        Scrabble scrabble = new Scrabble("alacrity");
64
+
65
+        assertEquals(scrabble.getScore(), 13);
66
+    }
67
+}