From 700eed1f6bdd417b9f34f00b7fa28483a046eecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Mon, 28 Apr 2025 22:04:25 -0700 Subject: [PATCH] Add `food-chain` --- config.json | 8 + .../practice/food-chain/.docs/instructions.md | 64 +++++++ .../practice/food-chain/.meta/config.json | 19 +++ .../practice/food-chain/.meta/example.coffee | 41 +++++ .../practice/food-chain/.meta/tests.toml | 40 +++++ .../practice/food-chain/food-chain.coffee | 4 + .../food-chain/food-chain.spec.coffee | 159 ++++++++++++++++++ 7 files changed, 335 insertions(+) create mode 100644 exercises/practice/food-chain/.docs/instructions.md create mode 100644 exercises/practice/food-chain/.meta/config.json create mode 100644 exercises/practice/food-chain/.meta/example.coffee create mode 100644 exercises/practice/food-chain/.meta/tests.toml create mode 100644 exercises/practice/food-chain/food-chain.coffee create mode 100644 exercises/practice/food-chain/food-chain.spec.coffee diff --git a/config.json b/config.json index bb81a2f..0bdbda1 100644 --- a/config.json +++ b/config.json @@ -201,6 +201,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "food-chain", + "name": "Food Chain", + "uuid": "32e9daaf-b58c-42a9-9b9b-8f54db58551f", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "gigasecond", "name": "Gigasecond", diff --git a/exercises/practice/food-chain/.docs/instructions.md b/exercises/practice/food-chain/.docs/instructions.md new file mode 100644 index 0000000..125820e --- /dev/null +++ b/exercises/practice/food-chain/.docs/instructions.md @@ -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 diff --git a/exercises/practice/food-chain/.meta/config.json b/exercises/practice/food-chain/.meta/config.json new file mode 100644 index 0000000..8cd1037 --- /dev/null +++ b/exercises/practice/food-chain/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "food-chain.coffee" + ], + "test": [ + "food-chain.spec.coffee" + ], + "example": [ + ".meta/example.coffee" + ] + }, + "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" +} diff --git a/exercises/practice/food-chain/.meta/example.coffee b/exercises/practice/food-chain/.meta/example.coffee new file mode 100644 index 0000000..7e1778f --- /dev/null +++ b/exercises/practice/food-chain/.meta/example.coffee @@ -0,0 +1,41 @@ +class FoodChain + @chain = [ + { animal: "fly", line: "I don't know why she swallowed the fly. Perhaps she'll die." } + { animal: "spider", line: "It wriggled and jiggled and tickled inside her." } + { animal: "bird", line: "How absurd to swallow a bird!" } + { animal: "cat", line: "Imagine that, to swallow a cat!" } + { animal: "dog", line: "What a hog, to swallow a dog!" } + { animal: "goat", line: "Just opened her throat and swallowed a goat!" } + { animal: "cow", line: "I don't know how she swallowed a cow!" } + { animal: "horse", line: "She's dead, of course!" } + ] + + @recite: (startVerse, endVerse) -> + result = [] + for verse in [startVerse..endVerse] + lines = [] + {animal, line} = @chain[verse - 1] + lines.push "I know an old lady who swallowed a #{animal}." + lines.push line + + if verse is 8 + result.push lines.join("\n") + continue + + if verse > 1 + for i in [verse - 1..1] + current = @chain[i] + previous = @chain[i - 1] + if i is 2 + lines.push "She swallowed the #{current.animal} to catch the #{previous.animal} that wriggled and jiggled and tickled inside her." + else + lines.push "She swallowed the #{current.animal} to catch the #{previous.animal}." + lines.push "I don't know why she swallowed the fly. Perhaps she'll die." + + if verse < endVerse + lines.push "" + + result.push lines.join "\n" + result.join "\n" + + module.exports = FoodChain diff --git a/exercises/practice/food-chain/.meta/tests.toml b/exercises/practice/food-chain/.meta/tests.toml new file mode 100644 index 0000000..30c5b98 --- /dev/null +++ b/exercises/practice/food-chain/.meta/tests.toml @@ -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" diff --git a/exercises/practice/food-chain/food-chain.coffee b/exercises/practice/food-chain/food-chain.coffee new file mode 100644 index 0000000..f8c97f3 --- /dev/null +++ b/exercises/practice/food-chain/food-chain.coffee @@ -0,0 +1,4 @@ +class FoodChain + @recite: (startVerse, endVerse) -> + + module.exports = FoodChain diff --git a/exercises/practice/food-chain/food-chain.spec.coffee b/exercises/practice/food-chain/food-chain.spec.coffee new file mode 100644 index 0000000..70ac8c7 --- /dev/null +++ b/exercises/practice/food-chain/food-chain.spec.coffee @@ -0,0 +1,159 @@ +FoodChain = require './food-chain' + +describe 'Food Chain', -> + it 'fly', -> + expected = [ + "I know an old lady who swallowed a fly." + "I don't know why she swallowed the fly. Perhaps she'll die." + ].join "\n" + expect(FoodChain.recite 1, 1).toEqual expected + + xit 'spider', -> + expected = [ + "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." + ].join "\n" + expect(FoodChain.recite 2, 2).toEqual expected + + xit 'bird', -> + expected = [ + "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." + ].join "\n" + expect(FoodChain.recite 3, 3).toEqual expected + + xit 'cat', -> + expected = [ + "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." + ].join "\n" + expect(FoodChain.recite 4, 4).toEqual expected + + xit 'dog', -> + expected = [ + "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." + ].join "\n" + expect(FoodChain.recite 5, 5).toEqual expected + + xit 'goat', -> + expected = [ + "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." + ].join "\n" + expect(FoodChain.recite 6, 6).toEqual expected + + xit 'cow', -> + expected = [ + "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." + ].join "\n" + expect(FoodChain.recite 7, 7).toEqual expected + + xit 'horse', -> + expected = [ + "I know an old lady who swallowed a horse." + "She's dead, of course!" + ].join "\n" + expect(FoodChain.recite 8, 8).toEqual expected + + xit 'multiple verses', -> + expected = [ + "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." + ].join "\n" + expect(FoodChain.recite 1, 3).toEqual expected + + xit 'full song', -> + expected = [ + "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!" + ].join "\n" + expect(FoodChain.recite 1, 8).toEqual expected +