forked from exercism/php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Food Chain Exercise (exercism#637)
- Loading branch information
1 parent
2b0ecd7
commit 3701616
Showing
7 changed files
with
461 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Instructions | ||
|
||
Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'. | ||
|
||
While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically. | ||
|
||
This is a [cumulative song][cumulative-song] of unknown origin. | ||
|
||
This is one of many common variants. | ||
|
||
```text | ||
I know an old lady who swallowed a fly. | ||
I don't know why she swallowed the fly. Perhaps she'll die. | ||
I know an old lady who swallowed a spider. | ||
It wriggled and jiggled and tickled inside her. | ||
She swallowed the spider to catch the fly. | ||
I don't know why she swallowed the fly. Perhaps she'll die. | ||
I know an old lady who swallowed a bird. | ||
How absurd to swallow a bird! | ||
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. | ||
She swallowed the spider to catch the fly. | ||
I don't know why she swallowed the fly. Perhaps she'll die. | ||
I know an old lady who swallowed a cat. | ||
Imagine that, to swallow a cat! | ||
She swallowed the cat to catch the bird. | ||
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. | ||
She swallowed the spider to catch the fly. | ||
I don't know why she swallowed the fly. Perhaps she'll die. | ||
I know an old lady who swallowed a dog. | ||
What a hog, to swallow a dog! | ||
She swallowed the dog to catch the cat. | ||
She swallowed the cat to catch the bird. | ||
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. | ||
She swallowed the spider to catch the fly. | ||
I don't know why she swallowed the fly. Perhaps she'll die. | ||
I know an old lady who swallowed a goat. | ||
Just opened her throat and swallowed a goat! | ||
She swallowed the goat to catch the dog. | ||
She swallowed the dog to catch the cat. | ||
She swallowed the cat to catch the bird. | ||
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. | ||
She swallowed the spider to catch the fly. | ||
I don't know why she swallowed the fly. Perhaps she'll die. | ||
I know an old lady who swallowed a cow. | ||
I don't know how she swallowed a cow! | ||
She swallowed the cow to catch the goat. | ||
She swallowed the goat to catch the dog. | ||
She swallowed the dog to catch the cat. | ||
She swallowed the cat to catch the bird. | ||
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her. | ||
She swallowed the spider to catch the fly. | ||
I don't know why she swallowed the fly. Perhaps she'll die. | ||
I know an old lady who swallowed a horse. | ||
She's dead, of course! | ||
``` | ||
|
||
[cumulative-song]: https://en.wikipedia.org/wiki/Cumulative_song |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"tomasnorre" | ||
], | ||
"files": { | ||
"solution": [ | ||
"FoodChain.php" | ||
], | ||
"test": [ | ||
"FoodChainTest.php" | ||
], | ||
"example": [ | ||
".meta/example.php" | ||
] | ||
}, | ||
"blurb": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
class FoodChain | ||
{ | ||
private const VERSE = [ | ||
["", ""], | ||
["fly", "I don't know why she swallowed the fly. Perhaps she'll die."], | ||
["spider", "It wriggled and jiggled and tickled inside her."], | ||
["bird", "How absurd to swallow a bird!"], | ||
["cat", "Imagine that, to swallow a cat!"], | ||
["dog", "What a hog, to swallow a dog!"], | ||
["goat", "Just opened her throat and swallowed a goat!"], | ||
["cow", "I don't know how she swallowed a cow!"], | ||
["horse", "She's dead, of course!"], | ||
]; | ||
|
||
public function verse(int $verseNumber): array | ||
{ | ||
$result = []; | ||
|
||
$result[] = sprintf("I know an old lady who swallowed a %s.", self::VERSE[$verseNumber][0]); | ||
$result[] = self::VERSE[$verseNumber][1]; | ||
if ($verseNumber === 1 || $verseNumber === 8) { | ||
return $result; | ||
} | ||
for (; $verseNumber > 1; $verseNumber--) { | ||
$text = sprintf( | ||
"She swallowed the %s to catch the %s%s", | ||
self::VERSE[$verseNumber][0], | ||
self::VERSE[$verseNumber - 1][0], | ||
$verseNumber !== 3 ? "." : "" | ||
); | ||
if ($verseNumber === 3) { | ||
$text .= " that wriggled and jiggled and tickled inside her."; | ||
} | ||
$result[] = $text; | ||
} | ||
$result[] = self::VERSE[$verseNumber][1]; | ||
return $result; | ||
} | ||
|
||
public function verses(int $start, int $end): array | ||
{ | ||
if ($start < 1 || $start > $end || $end > 8) { | ||
return $this->verse(1); | ||
} | ||
$s = $this->verse($start); | ||
while ($start < $end) { | ||
$start++; | ||
$s[] = ""; | ||
$s = array_merge($s, $this->verse($start)); | ||
} | ||
return $s; | ||
} | ||
|
||
public function song(): array | ||
{ | ||
return $this->verses(1, 8); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[751dce68-9412-496e-b6e8-855998c56166] | ||
description = "fly" | ||
|
||
[6c56f861-0c5e-4907-9a9d-b2efae389379] | ||
description = "spider" | ||
|
||
[3edf5f33-bef1-4e39-ae67-ca5eb79203fa] | ||
description = "bird" | ||
|
||
[e866a758-e1ff-400e-9f35-f27f28cc288f] | ||
description = "cat" | ||
|
||
[3f02c30e-496b-4b2a-8491-bc7e2953cafb] | ||
description = "dog" | ||
|
||
[4b3fd221-01ea-46e0-825b-5734634fbc59] | ||
description = "goat" | ||
|
||
[1b707da9-7001-4fac-941f-22ad9c7a65d4] | ||
description = "cow" | ||
|
||
[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc] | ||
description = "horse" | ||
|
||
[22b863d5-17e4-4d1e-93e4-617329a5c050] | ||
description = "multiple verses" | ||
|
||
[e626b32b-745c-4101-bcbd-3b13456893db] | ||
description = "full song" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* By adding type hints and enabling strict type checking, code can become | ||
* easier to read, self-documenting and reduce the number of potential bugs. | ||
* By default, type declarations are non-strict, which means they will attempt | ||
* to change the original type to match the type specified by the | ||
* type-declaration. | ||
* | ||
* In other words, if you pass a string to a function requiring a float, | ||
* it will attempt to convert the string value to a float. | ||
* | ||
* To enable strict mode, a single declare directive must be placed at the top | ||
* of the file. | ||
* This means that the strictness of typing is configured on a per-file basis. | ||
* This directive not only affects the type declarations of parameters, but also | ||
* a function's return type. | ||
* | ||
* For more info review the Concept on strict type checking in the PHP track | ||
* <link>. | ||
* | ||
* To disable strict typing, comment out the directive below. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
class FoodChain | ||
{ | ||
public function verse(int $verseNumber): array | ||
{ | ||
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); | ||
} | ||
|
||
public function verses(int $start, int $end): array | ||
{ | ||
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); | ||
} | ||
|
||
public function song(): array | ||
{ | ||
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); | ||
} | ||
} |
Oops, something went wrong.