Skip to content

Commit a7882c2

Browse files
authored
Add House Exercise (#647)
1 parent 24890e8 commit a7882c2

File tree

7 files changed

+654
-0
lines changed

7 files changed

+654
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,14 @@
11121112
"conditionals"
11131113
]
11141114
},
1115+
{
1116+
"slug": "house",
1117+
"name": "House",
1118+
"uuid": "76a067bc-5bc3-47de-a539-69eb6c80473a",
1119+
"practices": [],
1120+
"prerequisites": [],
1121+
"difficulty": 3
1122+
},
11151123
{
11161124
"slug": "scale-generator",
11171125
"name": "Scale Generator",
Lines changed: 105 additions & 0 deletions
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
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"tomasnorre"
4+
],
5+
"files": {
6+
"solution": [
7+
"House.php"
8+
],
9+
"test": [
10+
"HouseTest.php"
11+
],
12+
"example": [
13+
".meta/example.php"
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class House
6+
{
7+
private const OBJECTS = [
8+
'house',
9+
'malt',
10+
'rat',
11+
'cat',
12+
'dog',
13+
'cow with the crumpled horn',
14+
'maiden all forlorn',
15+
'man all tattered and torn',
16+
'priest all shaven and shorn',
17+
'rooster that crowed in the morn',
18+
'farmer sowing his corn',
19+
'horse and the hound and the horn',
20+
];
21+
22+
private const ACTIONS = [
23+
'Jack built',
24+
'lay in',
25+
'ate',
26+
'killed',
27+
'worried',
28+
'tossed',
29+
'milked',
30+
'kissed',
31+
'married',
32+
'woke',
33+
'kept',
34+
'belonged to',
35+
];
36+
37+
public function verse(int $verseNumber): array
38+
{
39+
$lines = [];
40+
$totalLines = $verseNumber;
41+
$itemIndex = $verseNumber - 1;
42+
for ($lineNumber = 1; $lineNumber <= $totalLines; $lineNumber++) {
43+
$lineText = '';
44+
if ($lineNumber === 1) {
45+
$lineText .= 'This is';
46+
} else {
47+
$lineText .= 'that ' . self::ACTIONS[$itemIndex];
48+
$itemIndex--;
49+
}
50+
$lineText .= ' the ' . self::OBJECTS[$itemIndex];
51+
if ($lineNumber === $totalLines) {
52+
$lineText .= ' that ' . self::ACTIONS[$itemIndex] . '.';
53+
}
54+
$lines[] = $lineText;
55+
}
56+
return $lines;
57+
}
58+
59+
public function verses(int $start, int $end): array
60+
{
61+
$lines = [];
62+
for ($i = $start; $i <= $end; $i++) {
63+
$verseLines = $this->verse($i);
64+
$lines = array_merge($lines, $verseLines);
65+
if ($i < $end) {
66+
$lines[] = '';
67+
}
68+
}
69+
return $lines;
70+
}
71+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[28a540ff-f765-4348-9d57-ae33f25f41f2]
13+
description = "verse one - the house that jack built"
14+
15+
[ebc825ac-6e2b-4a5e-9afd-95732191c8da]
16+
description = "verse two - the malt that lay"
17+
18+
[1ed8bb0f-edb8-4bd1-b6d4-b64754fe4a60]
19+
description = "verse three - the rat that ate"
20+
21+
[64b0954e-8b7d-4d14-aad0-d3f6ce297a30]
22+
description = "verse four - the cat that killed"
23+
24+
[1e8d56bc-fe31-424d-9084-61e6111d2c82]
25+
description = "verse five - the dog that worried"
26+
27+
[6312dc6f-ab0a-40c9-8a55-8d4e582beac4]
28+
description = "verse six - the cow with the crumpled horn"
29+
30+
[68f76d18-6e19-4692-819c-5ff6a7f92feb]
31+
description = "verse seven - the maiden all forlorn"
32+
33+
[73872564-2004-4071-b51d-2e4326096747]
34+
description = "verse eight - the man all tattered and torn"
35+
36+
[0d53d743-66cb-4351-a173-82702f3338c9]
37+
description = "verse nine - the priest all shaven and shorn"
38+
39+
[452f24dc-8fd7-4a82-be1a-3b4839cfeb41]
40+
description = "verse 10 - the rooster that crowed in the morn"
41+
42+
[97176f20-2dd3-4646-ac72-cffced91ea26]
43+
description = "verse 11 - the farmer sowing his corn"
44+
45+
[09824c29-6aad-4dcd-ac98-f61374a6a8b7]
46+
description = "verse 12 - the horse and the hound and the horn"
47+
48+
[d2b980d3-7851-49e1-97ab-1524515ec200]
49+
description = "multiple verses"
50+
51+
[0311d1d0-e085-4f23-8ae7-92406fb3e803]
52+
description = "full rhyme"

exercises/practice/house/House.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* By adding type hints and enabling strict type checking, code can become
5+
* easier to read, self-documenting and reduce the number of potential bugs.
6+
* By default, type declarations are non-strict, which means they will attempt
7+
* to change the original type to match the type specified by the
8+
* type-declaration.
9+
*
10+
* In other words, if you pass a string to a function requiring a float,
11+
* it will attempt to convert the string value to a float.
12+
*
13+
* To enable strict mode, a single declare directive must be placed at the top
14+
* of the file.
15+
* This means that the strictness of typing is configured on a per-file basis.
16+
* This directive not only affects the type declarations of parameters, but also
17+
* a function's return type.
18+
*
19+
* For more info review the Concept on strict type checking in the PHP track
20+
* <link>.
21+
*
22+
* To disable strict typing, comment out the directive below.
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
class House
28+
{
29+
public function verse(int $verseNumber): array
30+
{
31+
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
32+
}
33+
34+
public function verses(int $start, int $end): array
35+
{
36+
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
37+
}
38+
}

0 commit comments

Comments
 (0)