Skip to content

Commit 3701616

Browse files
authored
Add Food Chain Exercise (#637)
1 parent 2b0ecd7 commit 3701616

File tree

7 files changed

+461
-0
lines changed

7 files changed

+461
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,14 @@
11961196
"prerequisites": [],
11971197
"difficulty": 6
11981198
},
1199+
{
1200+
"slug": "food-chain",
1201+
"name": "Food Chain",
1202+
"uuid": "9fa82e62-1dd3-45c4-ae33-6c854a7b92b5",
1203+
"practices": [],
1204+
"prerequisites": [],
1205+
"difficulty": 4
1206+
},
11991207
{
12001208
"slug": "kindergarten-garden",
12011209
"name": "Kindergarten Garden",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Instructions
2+
3+
Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.
4+
5+
While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.
6+
7+
This is a [cumulative song][cumulative-song] of unknown origin.
8+
9+
This is one of many common variants.
10+
11+
```text
12+
I know an old lady who swallowed a fly.
13+
I don't know why she swallowed the fly. Perhaps she'll die.
14+
15+
I know an old lady who swallowed a spider.
16+
It wriggled and jiggled and tickled inside her.
17+
She swallowed the spider to catch the fly.
18+
I don't know why she swallowed the fly. Perhaps she'll die.
19+
20+
I know an old lady who swallowed a bird.
21+
How absurd to swallow a bird!
22+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
23+
She swallowed the spider to catch the fly.
24+
I don't know why she swallowed the fly. Perhaps she'll die.
25+
26+
I know an old lady who swallowed a cat.
27+
Imagine that, to swallow a cat!
28+
She swallowed the cat to catch the bird.
29+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
30+
She swallowed the spider to catch the fly.
31+
I don't know why she swallowed the fly. Perhaps she'll die.
32+
33+
I know an old lady who swallowed a dog.
34+
What a hog, to swallow a dog!
35+
She swallowed the dog to catch the cat.
36+
She swallowed the cat to catch the bird.
37+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
38+
She swallowed the spider to catch the fly.
39+
I don't know why she swallowed the fly. Perhaps she'll die.
40+
41+
I know an old lady who swallowed a goat.
42+
Just opened her throat and swallowed a goat!
43+
She swallowed the goat to catch the dog.
44+
She swallowed the dog to catch the cat.
45+
She swallowed the cat to catch the bird.
46+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
47+
She swallowed the spider to catch the fly.
48+
I don't know why she swallowed the fly. Perhaps she'll die.
49+
50+
I know an old lady who swallowed a cow.
51+
I don't know how she swallowed a cow!
52+
She swallowed the cow to catch the goat.
53+
She swallowed the goat to catch the dog.
54+
She swallowed the dog to catch the cat.
55+
She swallowed the cat to catch the bird.
56+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
57+
She swallowed the spider to catch the fly.
58+
I don't know why she swallowed the fly. Perhaps she'll die.
59+
60+
I know an old lady who swallowed a horse.
61+
She's dead, of course!
62+
```
63+
64+
[cumulative-song]: https://en.wikipedia.org/wiki/Cumulative_song
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+
"FoodChain.php"
8+
],
9+
"test": [
10+
"FoodChainTest.php"
11+
],
12+
"example": [
13+
".meta/example.php"
14+
]
15+
},
16+
"blurb": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly"
19+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class FoodChain
6+
{
7+
private const VERSE = [
8+
["", ""],
9+
["fly", "I don't know why she swallowed the fly. Perhaps she'll die."],
10+
["spider", "It wriggled and jiggled and tickled inside her."],
11+
["bird", "How absurd to swallow a bird!"],
12+
["cat", "Imagine that, to swallow a cat!"],
13+
["dog", "What a hog, to swallow a dog!"],
14+
["goat", "Just opened her throat and swallowed a goat!"],
15+
["cow", "I don't know how she swallowed a cow!"],
16+
["horse", "She's dead, of course!"],
17+
];
18+
19+
public function verse(int $verseNumber): array
20+
{
21+
$result = [];
22+
23+
$result[] = sprintf("I know an old lady who swallowed a %s.", self::VERSE[$verseNumber][0]);
24+
$result[] = self::VERSE[$verseNumber][1];
25+
if ($verseNumber === 1 || $verseNumber === 8) {
26+
return $result;
27+
}
28+
for (; $verseNumber > 1; $verseNumber--) {
29+
$text = sprintf(
30+
"She swallowed the %s to catch the %s%s",
31+
self::VERSE[$verseNumber][0],
32+
self::VERSE[$verseNumber - 1][0],
33+
$verseNumber !== 3 ? "." : ""
34+
);
35+
if ($verseNumber === 3) {
36+
$text .= " that wriggled and jiggled and tickled inside her.";
37+
}
38+
$result[] = $text;
39+
}
40+
$result[] = self::VERSE[$verseNumber][1];
41+
return $result;
42+
}
43+
44+
public function verses(int $start, int $end): array
45+
{
46+
if ($start < 1 || $start > $end || $end > 8) {
47+
return $this->verse(1);
48+
}
49+
$s = $this->verse($start);
50+
while ($start < $end) {
51+
$start++;
52+
$s[] = "";
53+
$s = array_merge($s, $this->verse($start));
54+
}
55+
return $s;
56+
}
57+
58+
public function song(): array
59+
{
60+
return $this->verses(1, 8);
61+
}
62+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
[751dce68-9412-496e-b6e8-855998c56166]
13+
description = "fly"
14+
15+
[6c56f861-0c5e-4907-9a9d-b2efae389379]
16+
description = "spider"
17+
18+
[3edf5f33-bef1-4e39-ae67-ca5eb79203fa]
19+
description = "bird"
20+
21+
[e866a758-e1ff-400e-9f35-f27f28cc288f]
22+
description = "cat"
23+
24+
[3f02c30e-496b-4b2a-8491-bc7e2953cafb]
25+
description = "dog"
26+
27+
[4b3fd221-01ea-46e0-825b-5734634fbc59]
28+
description = "goat"
29+
30+
[1b707da9-7001-4fac-941f-22ad9c7a65d4]
31+
description = "cow"
32+
33+
[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc]
34+
description = "horse"
35+
36+
[22b863d5-17e4-4d1e-93e4-617329a5c050]
37+
description = "multiple verses"
38+
39+
[e626b32b-745c-4101-bcbd-3b13456893db]
40+
description = "full song"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 FoodChain
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+
39+
public function song(): array
40+
{
41+
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
42+
}
43+
}

0 commit comments

Comments
 (0)