Skip to content

Commit 899327b

Browse files
Add house exercise (#241)
1 parent f085d44 commit 899327b

File tree

7 files changed

+284
-0
lines changed

7 files changed

+284
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,14 @@
479479
"prerequisites": [],
480480
"difficulty": 4
481481
},
482+
{
483+
"slug": "house",
484+
"name": "House",
485+
"uuid": "d20845f7-bd86-4b0a-8101-8644388ac312",
486+
"practices": [],
487+
"prerequisites": [],
488+
"difficulty": 4
489+
},
482490
{
483491
"slug": "killer-sudoku-helper",
484492
"name": "Killer Sudoku Helper",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Instructions
2+
3+
Recite the nursery rhyme 'This is the House that Jack Built'.
4+
5+
> [The] process of placing a phrase of clause within another phrase of clause is called embedding.
6+
> It is through the processes of recursion and embedding that we are able to take a finite number of forms (words and phrases) and construct an infinite number of expressions.
7+
> Furthermore, embedding also allows us to construct an infinitely long structure, in theory anyway.
8+
9+
- [papyr.com][papyr]
10+
11+
The nursery rhyme reads as follows:
12+
13+
```text
14+
This is the house that Jack built.
15+
16+
This is the malt
17+
that lay in the house that Jack built.
18+
19+
This is the rat
20+
that ate the malt
21+
that lay in the house that Jack built.
22+
23+
This is the cat
24+
that killed the rat
25+
that ate the malt
26+
that lay in the house that Jack built.
27+
28+
This is the dog
29+
that worried the cat
30+
that killed the rat
31+
that ate the malt
32+
that lay in the house that Jack built.
33+
34+
This is the cow with the crumpled horn
35+
that tossed the dog
36+
that worried the cat
37+
that killed the rat
38+
that ate the malt
39+
that lay in the house that Jack built.
40+
41+
This is the maiden all forlorn
42+
that milked the cow with the crumpled horn
43+
that tossed the dog
44+
that worried the cat
45+
that killed the rat
46+
that ate the malt
47+
that lay in the house that Jack built.
48+
49+
This is the man all tattered and torn
50+
that kissed the maiden all forlorn
51+
that milked the cow with the crumpled horn
52+
that tossed the dog
53+
that worried the cat
54+
that killed the rat
55+
that ate the malt
56+
that lay in the house that Jack built.
57+
58+
This is the priest all shaven and shorn
59+
that married the man all tattered and torn
60+
that kissed the maiden all forlorn
61+
that milked the cow with the crumpled horn
62+
that tossed the dog
63+
that worried the cat
64+
that killed the rat
65+
that ate the malt
66+
that lay in the house that Jack built.
67+
68+
This is the rooster that crowed in the morn
69+
that woke the priest all shaven and shorn
70+
that married the man all tattered and torn
71+
that kissed the maiden all forlorn
72+
that milked the cow with the crumpled horn
73+
that tossed the dog
74+
that worried the cat
75+
that killed the rat
76+
that ate the malt
77+
that lay in the house that Jack built.
78+
79+
This is the farmer sowing his corn
80+
that kept the rooster that crowed in the morn
81+
that woke the priest all shaven and shorn
82+
that married the man all tattered and torn
83+
that kissed the maiden all forlorn
84+
that milked the cow with the crumpled horn
85+
that tossed the dog
86+
that worried the cat
87+
that killed the rat
88+
that ate the malt
89+
that lay in the house that Jack built.
90+
91+
This is the horse and the hound and the horn
92+
that belonged to the farmer sowing his corn
93+
that kept the rooster that crowed in the morn
94+
that woke the priest all shaven and shorn
95+
that married the man all tattered and torn
96+
that kissed the maiden all forlorn
97+
that milked the cow with the crumpled horn
98+
that tossed the dog
99+
that worried the cat
100+
that killed the rat
101+
that ate the malt
102+
that lay in the house that Jack built.
103+
```
104+
105+
[papyr]: https://papyr.com/hypertextbooks/grammar/ph_noun.htm
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"house.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
14+
]
15+
},
16+
"blurb": "Output the nursery rhyme 'This is the House that Jack Built'.",
17+
"source": "British nursery rhyme",
18+
"source_url": "https://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built"
19+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module main
2+
3+
import strings
4+
5+
const lyrics = 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.\n'
6+
7+
const table = [0, 389, 368, 351, 331, 310, 267, 232, 190, 145, 99, 62, 8]
8+
9+
fn recite(start_verse int, end_verse int) string {
10+
mut builder := strings.new_builder(4000)
11+
for verse in start_verse .. (end_verse + 1) {
12+
builder.write_string(lyrics[0..8])
13+
builder.write_string(lyrics[table[verse]..])
14+
}
15+
builder.go_back(1) // Omit final newline
16+
return builder.str()
17+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[28a540ff-f765-4348-9d57-ae33f25f41f2]
6+
description = "verse one - the house that jack built"
7+
8+
[ebc825ac-6e2b-4a5e-9afd-95732191c8da]
9+
description = "verse two - the malt that lay"
10+
11+
[1ed8bb0f-edb8-4bd1-b6d4-b64754fe4a60]
12+
description = "verse three - the rat that ate"
13+
14+
[64b0954e-8b7d-4d14-aad0-d3f6ce297a30]
15+
description = "verse four - the cat that killed"
16+
17+
[1e8d56bc-fe31-424d-9084-61e6111d2c82]
18+
description = "verse five - the dog that worried"
19+
20+
[6312dc6f-ab0a-40c9-8a55-8d4e582beac4]
21+
description = "verse six - the cow with the crumpled horn"
22+
23+
[68f76d18-6e19-4692-819c-5ff6a7f92feb]
24+
description = "verse seven - the maiden all forlorn"
25+
26+
[73872564-2004-4071-b51d-2e4326096747]
27+
description = "verse eight - the man all tattered and torn"
28+
29+
[0d53d743-66cb-4351-a173-82702f3338c9]
30+
description = "verse nine - the priest all shaven and shorn"
31+
32+
[452f24dc-8fd7-4a82-be1a-3b4839cfeb41]
33+
description = "verse 10 - the rooster that crowed in the morn"
34+
35+
[97176f20-2dd3-4646-ac72-cffced91ea26]
36+
description = "verse 11 - the farmer sowing his corn"
37+
38+
[09824c29-6aad-4dcd-ac98-f61374a6a8b7]
39+
description = "verse 12 - the horse and the hound and the horn"
40+
41+
[d2b980d3-7851-49e1-97ab-1524515ec200]
42+
description = "multiple verses"
43+
44+
[0311d1d0-e085-4f23-8ae7-92406fb3e803]
45+
description = "full rhyme"

exercises/practice/house/house.v

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module main
2+
3+
fn recite(start_verse int, end_verse int) string {
4+
}

exercises/practice/house/run_test.v

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
module main
2+
3+
fn test_verse_one___the_house_that_jack_built() {
4+
expected := 'This is the house that Jack built.'
5+
assert recite(1, 1) == expected
6+
}
7+
8+
fn test_verse_two___the_malt_that_lay() {
9+
expected := 'This is the malt that lay in the house that Jack built.'
10+
assert recite(2, 2) == expected
11+
}
12+
13+
fn test_verse_three___the_rat_that_ate() {
14+
expected := 'This is the rat that ate the malt that lay in the house that Jack built.'
15+
assert recite(3, 3) == expected
16+
}
17+
18+
fn test_verse_four___the_cat_that_killed() {
19+
expected := 'This is the cat that killed the rat that ate the malt that lay in the house that Jack built.'
20+
assert recite(4, 4) == expected
21+
}
22+
23+
fn test_verse_five___the_dog_that_worried() {
24+
expected := 'This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
25+
assert recite(5, 5) == expected
26+
}
27+
28+
fn test_verse_six___the_cow_with_the_crumpled_horn() {
29+
expected := 'This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
30+
assert recite(6, 6) == expected
31+
}
32+
33+
fn test_verse_seven___the_maiden_all_forlorn() {
34+
expected := 'This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
35+
assert recite(7, 7) == expected
36+
}
37+
38+
fn test_verse_eight___the_man_all_tattered_and_torn() {
39+
expected := 'This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
40+
assert recite(8, 8) == expected
41+
}
42+
43+
fn test_verse_nine___the_priest_all_shaven_and_shorn() {
44+
expected := 'This is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
45+
assert recite(9, 9) == expected
46+
}
47+
48+
fn test_verse_10___the_rooster_that_crowed_in_the_morn() {
49+
expected := 'This is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
50+
assert recite(10, 10) == expected
51+
}
52+
53+
fn test_verse_11___the_farmer_sowing_his_corn() {
54+
expected := 'This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
55+
assert recite(11, 11) == expected
56+
}
57+
58+
fn test_verse_12___the_horse_and_the_hound_and_the_horn() {
59+
expected := 'This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.'
60+
assert recite(12, 12) == expected
61+
}
62+
63+
fn test_multiple_verses() {
64+
expected := ('This is the cat that killed the rat that ate the malt that lay in the house that Jack built.
65+
This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
66+
This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
67+
This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
68+
This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.')
69+
assert recite(4, 8) == expected
70+
}
71+
72+
fn test_full_rhyme() {
73+
expected := ('This is the house that Jack built.
74+
This is the malt that lay in the house that Jack built.
75+
This is the rat that ate the malt that lay in the house that Jack built.
76+
This is the cat that killed the rat that ate the malt that lay in the house that Jack built.
77+
This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
78+
This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
79+
This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
80+
This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
81+
This is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
82+
This is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
83+
This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
84+
This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.')
85+
assert recite(1, 12) == expected
86+
}

0 commit comments

Comments
 (0)