瀏覽代碼

Merge branch 'master' into ChooseCoreExercises

Frida Tveit 9 年之前
父節點
當前提交
5478ad9696

+ 10
- 0
config.json 查看文件

@@ -443,6 +443,16 @@
443 443
       ]
444 444
     },
445 445
     {
446
+      "uuid": "6b51aca3-3451-4a64-ad7b-00b151d7548a",
447
+      "slug": "house",
448
+      "core": false, 
449
+      "unlocked_by": null,
450
+      "difficulty": 6,
451
+      "topics": [
452
+
453
+      ]
454
+    },
455
+    {
446 456
       "uuid": "dd3e6fd6-5359-4978-acd7-c39374cead4d",
447 457
       "slug": "food-chain",
448 458
       "core": false,

+ 124
- 0
exercises/house/README.md 查看文件

@@ -0,0 +1,124 @@
1
+# House
2
+
3
+Output the nursery rhyme 'This is the House that Jack Built'.
4
+
5
+> [The] process of placing a phrase of clause within another phrase of
6
+> clause is called embedding. It is through the processes of recursion
7
+> and embedding that we are able to take a finite number of forms (words
8
+> and phrases) and construct an infinite number of expressions.
9
+> Furthermore, embedding also allows us to construct an infinitely long
10
+> structure, in theory anyway.
11
+
12
+- [papyr.com](http://papyr.com/hypertextbooks/grammar/ph_noun.htm)
13
+
14
+
15
+The nursery rhyme reads as follows:
16
+
17
+```plain
18
+This is the house that Jack built.
19
+
20
+This is the malt
21
+that lay in the house that Jack built.
22
+
23
+This is the rat
24
+that ate the malt
25
+that lay in the house that Jack built.
26
+
27
+This is the cat
28
+that killed the rat
29
+that ate the malt
30
+that lay in the house that Jack built.
31
+
32
+This is the dog
33
+that worried the cat
34
+that killed the rat
35
+that ate the malt
36
+that lay in the house that Jack built.
37
+
38
+This is the cow with the crumpled horn
39
+that tossed the dog
40
+that worried the cat
41
+that killed the rat
42
+that ate the malt
43
+that lay in the house that Jack built.
44
+
45
+This is the maiden all forlorn
46
+that milked the cow with the crumpled horn
47
+that tossed the dog
48
+that worried the cat
49
+that killed the rat
50
+that ate the malt
51
+that lay in the house that Jack built.
52
+
53
+This is the man all tattered and torn
54
+that kissed the maiden all forlorn
55
+that milked the cow with the crumpled horn
56
+that tossed the dog
57
+that worried the cat
58
+that killed the rat
59
+that ate the malt
60
+that lay in the house that Jack built.
61
+
62
+This is the priest all shaven and shorn
63
+that married the man all tattered and torn
64
+that kissed the maiden all forlorn
65
+that milked the cow with the crumpled horn
66
+that tossed the dog
67
+that worried the cat
68
+that killed the rat
69
+that ate the malt
70
+that lay in the house that Jack built.
71
+
72
+This is the rooster that crowed in the morn
73
+that woke the priest all shaven and shorn
74
+that married the man all tattered and torn
75
+that kissed the maiden all forlorn
76
+that milked the cow with the crumpled horn
77
+that tossed the dog
78
+that worried the cat
79
+that killed the rat
80
+that ate the malt
81
+that lay in the house that Jack built.
82
+
83
+This is the farmer sowing his corn
84
+that kept the rooster that crowed in the morn
85
+that woke the priest all shaven and shorn
86
+that married the man all tattered and torn
87
+that kissed the maiden all forlorn
88
+that milked the cow with the crumpled horn
89
+that tossed the dog
90
+that worried the cat
91
+that killed the rat
92
+that ate the malt
93
+that lay in the house that Jack built.
94
+
95
+This is the horse and the hound and the horn
96
+that belonged to the farmer sowing his corn
97
+that kept the rooster that crowed in the morn
98
+that woke the priest all shaven and shorn
99
+that married the man all tattered and torn
100
+that kissed the maiden all forlorn
101
+that milked the cow with the crumpled horn
102
+that tossed the dog
103
+that worried the cat
104
+that killed the rat
105
+that ate the malt
106
+that lay in the house that Jack built.
107
+```
108
+
109
+
110
+To run the tests:
111
+
112
+```sh
113
+$ gradle test
114
+```
115
+
116
+For more detailed info about the Java track see the [help page](http://exercism.io/languages/java).
117
+
118
+
119
+## Source
120
+
121
+British nursery rhyme [http://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built](http://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built)
122
+
123
+## Submitting Incomplete Solutions
124
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.

+ 18
- 0
exercises/house/build.gradle 查看文件

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

+ 55
- 0
exercises/house/src/example/java/House.java 查看文件

@@ -0,0 +1,55 @@
1
+class House {
2
+    private static final String[] CHARACTERS = {
3
+            "house that Jack built.",
4
+            "malt",
5
+            "rat",
6
+            "cat",
7
+            "dog",
8
+            "cow with the crumpled horn",
9
+            "maiden all forlorn",
10
+            "man all tattered and torn",
11
+            "priest all shaven and shorn",
12
+            "rooster that crowed in the morn",
13
+            "farmer sowing his corn",
14
+            "horse and the hound and the horn"
15
+    };
16
+
17
+    private static final String[] ACTIONS = {
18
+            "lay in",
19
+            "ate",
20
+            "killed",
21
+            "worried",
22
+            "tossed",
23
+            "milked",
24
+            "kissed",
25
+            "married",
26
+            "woke",
27
+            "kept",
28
+            "belonged to"
29
+    };
30
+
31
+    String verse(int verseNumber) {
32
+        StringBuilder verse = new StringBuilder();
33
+        verse.append("This is the " + CHARACTERS[verseNumber - 1]);
34
+
35
+        for (int i = verseNumber - 2; i >= 0; i--) {
36
+            verse.append("\nthat " + ACTIONS[i] + " the " + CHARACTERS[i]);
37
+        }
38
+
39
+        return verse.toString();
40
+    }
41
+
42
+    String verses(int startVerse, int endVerse) {
43
+        String[] verses = new String[endVerse - startVerse + 1];
44
+
45
+        for (int i = startVerse; i <= endVerse; i++) {
46
+            verses[i - startVerse] = verse(i);
47
+        }
48
+
49
+        return String.join("\n\n", verses);
50
+    }
51
+
52
+    String sing() {
53
+        return verses(1, 12);
54
+    }
55
+}

+ 0
- 0
exercises/house/src/main/java/.keep 查看文件


+ 341
- 0
exercises/house/src/test/java/HouseTest.java 查看文件

@@ -0,0 +1,341 @@
1
+import org.junit.Test;
2
+import org.junit.Ignore;
3
+import org.junit.Before;
4
+
5
+import static org.junit.Assert.assertEquals;
6
+
7
+public class HouseTest {
8
+
9
+    private House house;
10
+
11
+    @Before
12
+    public void setup() {
13
+        house = new House();
14
+    }
15
+
16
+    @Test
17
+    public void verseOne() {
18
+        String expected = "This is the house that Jack built.";
19
+        int verse = 1;
20
+
21
+        assertEquals(expected, house.verse(verse));
22
+    }
23
+
24
+    @Ignore("Remove to run test")
25
+    @Test
26
+    public void verseTwo() {
27
+        String expected =
28
+                "This is the malt\n" +
29
+                "that lay in the house that Jack built.";
30
+        int verse = 2;
31
+
32
+        assertEquals(expected, house.verse(verse));
33
+    }
34
+
35
+    @Ignore("Remove to run test")
36
+    @Test
37
+    public void verseThree() {
38
+        String expected =
39
+                "This is the rat\n" +
40
+                "that ate the malt\n" +
41
+                "that lay in the house that Jack built.";
42
+        int verse = 3;
43
+
44
+        assertEquals(expected, house.verse(verse));
45
+    }
46
+
47
+    @Ignore("Remove to run test")
48
+    @Test
49
+    public void verseFour() {
50
+        String expected =
51
+                "This is the cat\n" +
52
+                "that killed the rat\n" +
53
+                "that ate the malt\n" +
54
+                "that lay in the house that Jack built.";
55
+        int verse = 4;
56
+
57
+        assertEquals(expected, house.verse(verse));
58
+    }
59
+
60
+    @Ignore("Remove to run test")
61
+    @Test
62
+    public void verseFive() {
63
+        String expected =
64
+                "This is the dog\n" +
65
+                "that worried the cat\n" +
66
+                "that killed the rat\n" +
67
+                "that ate the malt\n" +
68
+                "that lay in the house that Jack built.";
69
+        int verse = 5;
70
+
71
+        assertEquals(expected, house.verse(verse));
72
+    }
73
+
74
+    @Ignore("Remove to run test")
75
+    @Test
76
+    public void verseSix() {
77
+        String expected =
78
+                "This is the cow with the crumpled horn\n" +
79
+                "that tossed the dog\n" +
80
+                "that worried the cat\n" +
81
+                "that killed the rat\n" +
82
+                "that ate the malt\n" +
83
+                "that lay in the house that Jack built.";
84
+        int verse = 6;
85
+
86
+        assertEquals(expected, house.verse(verse));
87
+    }
88
+
89
+    @Ignore("Remove to run test")
90
+    @Test
91
+    public void verseSeven() {
92
+        String expected =
93
+                "This is the maiden all forlorn\n" +
94
+                "that milked the cow with the crumpled horn\n" +
95
+                "that tossed the dog\n" +
96
+                "that worried the cat\n" +
97
+                "that killed the rat\n" +
98
+                "that ate the malt\n" +
99
+                "that lay in the house that Jack built.";
100
+        int verse = 7;
101
+
102
+        assertEquals(expected, house.verse(verse));
103
+    }
104
+
105
+    @Ignore("Remove to run test")
106
+    @Test
107
+    public void verseEight() {
108
+        String expected =
109
+                "This is the man all tattered and torn\n" +
110
+                "that kissed the maiden all forlorn\n" +
111
+                "that milked the cow with the crumpled horn\n" +
112
+                "that tossed the dog\n" +
113
+                "that worried the cat\n" +
114
+                "that killed the rat\n" +
115
+                "that ate the malt\n" +
116
+                "that lay in the house that Jack built.";
117
+        int verse = 8;
118
+
119
+        assertEquals(expected, house.verse(verse));
120
+    }
121
+
122
+    @Ignore("Remove to run test")
123
+    @Test
124
+    public void verseNine() {
125
+        String expected =
126
+                "This is the priest all shaven and shorn\n" +
127
+                "that married the man all tattered and torn\n" +
128
+                "that kissed the maiden all forlorn\n" +
129
+                "that milked the cow with the crumpled horn\n" +
130
+                "that tossed the dog\n" +
131
+                "that worried the cat\n" +
132
+                "that killed the rat\n" +
133
+                "that ate the malt\n" +
134
+                "that lay in the house that Jack built.";
135
+        int verse = 9;
136
+
137
+        assertEquals(expected, house.verse(verse));
138
+    }
139
+
140
+    @Ignore("Remove to run test")
141
+    @Test
142
+    public void verse10() {
143
+        String expected =
144
+                "This is the rooster that crowed in the morn\n" +
145
+                "that woke the priest all shaven and shorn\n" +
146
+                "that married the man all tattered and torn\n" +
147
+                "that kissed the maiden all forlorn\n" +
148
+                "that milked the cow with the crumpled horn\n" +
149
+                "that tossed the dog\n" +
150
+                "that worried the cat\n" +
151
+                "that killed the rat\n" +
152
+                "that ate the malt\n" +
153
+                "that lay in the house that Jack built.";
154
+        int verse = 10;
155
+
156
+        assertEquals(expected, house.verse(verse));
157
+    }
158
+
159
+    @Ignore("Remove to run test")
160
+    @Test
161
+    public void verse11() {
162
+        String expected =
163
+                "This is the farmer sowing his corn\n" +
164
+                "that kept the rooster that crowed in the morn\n" +
165
+                "that woke the priest all shaven and shorn\n" +
166
+                "that married the man all tattered and torn\n" +
167
+                "that kissed the maiden all forlorn\n" +
168
+                "that milked the cow with the crumpled horn\n" +
169
+                "that tossed the dog\n" +
170
+                "that worried the cat\n" +
171
+                "that killed the rat\n" +
172
+                "that ate the malt\n" +
173
+                "that lay in the house that Jack built.";
174
+        int verse = 11;
175
+
176
+        assertEquals(expected, house.verse(verse));
177
+    }
178
+
179
+    @Ignore("Remove to run test")
180
+    @Test
181
+    public void verse12() {
182
+        String expected =
183
+                "This is the horse and the hound and the horn\n" +
184
+                "that belonged to the farmer sowing his corn\n" +
185
+                "that kept the rooster that crowed in the morn\n" +
186
+                "that woke the priest all shaven and shorn\n" +
187
+                "that married the man all tattered and torn\n" +
188
+                "that kissed the maiden all forlorn\n" +
189
+                "that milked the cow with the crumpled horn\n" +
190
+                "that tossed the dog\n" +
191
+                "that worried the cat\n" +
192
+                "that killed the rat\n" +
193
+                "that ate the malt\n" +
194
+                "that lay in the house that Jack built.";
195
+        int verse = 12;
196
+
197
+        assertEquals(expected, house.verse(verse));
198
+    }
199
+
200
+    @Ignore("Remove to run test")
201
+    @Test
202
+    public void multipleVerses() {
203
+        String expected =
204
+                "This is the cat\n" +
205
+                "that killed the rat\n" +
206
+                "that ate the malt\n" +
207
+                "that lay in the house that Jack built.\n" +
208
+                "\n" +
209
+                "This is the dog\n" +
210
+                "that worried the cat\n" +
211
+                "that killed the rat\n" +
212
+                "that ate the malt\n" +
213
+                "that lay in the house that Jack built.\n" +
214
+                "\n" +
215
+                "This is the cow with the crumpled horn\n" +
216
+                "that tossed the dog\n" +
217
+                "that worried the cat\n" +
218
+                "that killed the rat\n" +
219
+                "that ate the malt\n" +
220
+                "that lay in the house that Jack built.\n" +
221
+                "\n" +
222
+                "This is the maiden all forlorn\n" +
223
+                "that milked the cow with the crumpled horn\n" +
224
+                "that tossed the dog\n" +
225
+                "that worried the cat\n" +
226
+                "that killed the rat\n" +
227
+                "that ate the malt\n" +
228
+                "that lay in the house that Jack built.\n" +
229
+                "\n" +
230
+                "This is the man all tattered and torn\n" +
231
+                "that kissed the maiden all forlorn\n" +
232
+                "that milked the cow with the crumpled horn\n" +
233
+                "that tossed the dog\n" +
234
+                "that worried the cat\n" +
235
+                "that killed the rat\n" +
236
+                "that ate the malt\n" +
237
+                "that lay in the house that Jack built.";
238
+
239
+        int startVerse = 4;
240
+        int endVerse = 8;
241
+
242
+        assertEquals(expected, house.verses(startVerse, endVerse));
243
+    }
244
+
245
+    @Ignore("Remove to run test")
246
+    @Test
247
+    public void wholeRhyme() {
248
+        String expected =
249
+                "This is the house that Jack built.\n" +
250
+                "\n" +
251
+                "This is the malt\n" +
252
+                "that lay in the house that Jack built.\n" +
253
+                "\n" +
254
+                "This is the rat\n" +
255
+                "that ate the malt\n" +
256
+                "that lay in the house that Jack built.\n" +
257
+                "\n" +
258
+                "This is the cat\n" +
259
+                "that killed the rat\n" +
260
+                "that ate the malt\n" +
261
+                "that lay in the house that Jack built.\n" +
262
+                "\n" +
263
+                "This is the dog\n" +
264
+                "that worried the cat\n" +
265
+                "that killed the rat\n" +
266
+                "that ate the malt\n" +
267
+                "that lay in the house that Jack built.\n" +
268
+                "\n" +
269
+                "This is the cow with the crumpled horn\n" +
270
+                "that tossed the dog\n" +
271
+                "that worried the cat\n" +
272
+                "that killed the rat\n" +
273
+                "that ate the malt\n" +
274
+                "that lay in the house that Jack built.\n" +
275
+                "\n" +
276
+                "This is the maiden all forlorn\n" +
277
+                "that milked the cow with the crumpled horn\n" +
278
+                "that tossed the dog\n" +
279
+                "that worried the cat\n" +
280
+                "that killed the rat\n" +
281
+                "that ate the malt\n" +
282
+                "that lay in the house that Jack built.\n" +
283
+                "\n" +
284
+                "This is the man all tattered and torn\n" +
285
+                "that kissed the maiden all forlorn\n" +
286
+                "that milked the cow with the crumpled horn\n" +
287
+                "that tossed the dog\n" +
288
+                "that worried the cat\n" +
289
+                "that killed the rat\n" +
290
+                "that ate the malt\n" +
291
+                "that lay in the house that Jack built.\n" +
292
+                "\n" +
293
+                "This is the priest all shaven and shorn\n" +
294
+                "that married the man all tattered and torn\n" +
295
+                "that kissed the maiden all forlorn\n" +
296
+                "that milked the cow with the crumpled horn\n" +
297
+                "that tossed the dog\n" +
298
+                "that worried the cat\n" +
299
+                "that killed the rat\n" +
300
+                "that ate the malt\n" +
301
+                "that lay in the house that Jack built.\n" +
302
+                "\n" +
303
+                "This is the rooster that crowed in the morn\n" +
304
+                "that woke the priest all shaven and shorn\n" +
305
+                "that married the man all tattered and torn\n" +
306
+                "that kissed the maiden all forlorn\n" +
307
+                "that milked the cow with the crumpled horn\n" +
308
+                "that tossed the dog\n" +
309
+                "that worried the cat\n" +
310
+                "that killed the rat\n" +
311
+                "that ate the malt\n" +
312
+                "that lay in the house that Jack built.\n" +
313
+                "\n" +
314
+                "This is the farmer sowing his corn\n" +
315
+                "that kept the rooster that crowed in the morn\n" +
316
+                "that woke the priest all shaven and shorn\n" +
317
+                "that married the man all tattered and torn\n" +
318
+                "that kissed the maiden all forlorn\n" +
319
+                "that milked the cow with the crumpled horn\n" +
320
+                "that tossed the dog\n" +
321
+                "that worried the cat\n" +
322
+                "that killed the rat\n" +
323
+                "that ate the malt\n" +
324
+                "that lay in the house that Jack built.\n" +
325
+                "\n" +
326
+                "This is the horse and the hound and the horn\n" +
327
+                "that belonged to the farmer sowing his corn\n" +
328
+                "that kept the rooster that crowed in the morn\n" +
329
+                "that woke the priest all shaven and shorn\n" +
330
+                "that married the man all tattered and torn\n" +
331
+                "that kissed the maiden all forlorn\n" +
332
+                "that milked the cow with the crumpled horn\n" +
333
+                "that tossed the dog\n" +
334
+                "that worried the cat\n" +
335
+                "that killed the rat\n" +
336
+                "that ate the malt\n" +
337
+                "that lay in the house that Jack built.";
338
+
339
+        assertEquals(expected, house.sing());
340
+    }
341
+}

+ 1
- 0
exercises/settings.gradle 查看文件

@@ -31,6 +31,7 @@ include 'grade-school'
31 31
 include 'hamming'
32 32
 include 'hexadecimal'
33 33
 include 'hello-world'
34
+include 'house'
34 35
 include 'isogram'
35 36
 include 'kindergarten-garden'
36 37
 include 'largest-series-product'