From 4014b29e5277a5a323e1f0106f09efe270b1c91d Mon Sep 17 00:00:00 2001 From: Kah Goh Date: Sun, 27 Oct 2024 17:15:13 +0800 Subject: [PATCH] Add food-chain exercise --- config.json | 8 +++ .../practice/food-chain/.docs/instructions.md | 64 +++++++++++++++++++ .../practice/food-chain/.meta/config.json | 19 ++++++ .../practice/food-chain/.meta/example.vim | 56 ++++++++++++++++ .../practice/food-chain/.meta/tests.toml | 40 ++++++++++++ .../practice/food-chain/food_chain.vader | 60 +++++++++++++++++ exercises/practice/food-chain/food_chain.vim | 7 ++ 7 files changed, 254 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.vim create mode 100644 exercises/practice/food-chain/.meta/tests.toml create mode 100644 exercises/practice/food-chain/food_chain.vader create mode 100644 exercises/practice/food-chain/food_chain.vim diff --git a/config.json b/config.json index 2f2c238..dc5ee3c 100644 --- a/config.json +++ b/config.json @@ -712,6 +712,14 @@ "practices": [], "prerequisites": [], "difficulty": 6 + }, + { + "slug": "food-chain", + "name": "Food Chain", + "uuid": "adf3945f-6735-471c-b02b-b3dac0aab1bc", + "practices": [], + "prerequisites": [], + "difficulty": 3 } ], "foregone": [ 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..a19ab1c --- /dev/null +++ b/exercises/practice/food-chain/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "kahgoh" + ], + "files": { + "solution": [ + "food_chain.vim" + ], + "test": [ + "food_chain.vader" + ], + "example": [ + ".meta/example.vim" + ] + }, + "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.vim b/exercises/practice/food-chain/.meta/example.vim new file mode 100644 index 0000000..548ab7e --- /dev/null +++ b/exercises/practice/food-chain/.meta/example.vim @@ -0,0 +1,56 @@ +let creatures=['fly', 'spider', 'bird', 'cat', 'dog', 'goat', 'cow', 'horse'] + +let uniqueLine={ + \ '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!" + \ } + +function! Verse(num) + let i = a:num + let creature = g:creatures[l:i - 1] + let lines = [ + \ 'I know an old lady who swallowed a ' . l:creature . '.', + \ g:uniqueLine[l:creature] + \ ] + + if l:creature ==# 'fly' || l:creature ==# 'horse' + return l:lines + endif + + while l:i > 1 + let creature1 = g:creatures[l:i - 1] + let creature2 = g:creatures[l:i - 2] + if l:creature2 ==# 'spider' + let lines = add(l:lines, 'She swallowed the ' . creature1 . ' to catch the ' . creature2 . ' that wriggled and jiggled and tickled inside her.') + else + let lines = add(l:lines, 'She swallowed the ' . creature1 . ' to catch the ' . creature2 . '.') + endif + let i -= 1 + endwhile + + return add(l:lines, g:uniqueLine['fly']) +endfunction + +function! Recite(endVerse, startVerse) + let content=[] + let num = a:startVerse + let break = 0 + + while l:num <= a:endVerse + if l:break + let content = add(l:content, '') + endif + let content += Verse(l:num) + + let num += 1 + let break = 1 + endwhile + + return content +endfunction 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.vader b/exercises/practice/food-chain/food_chain.vader new file mode 100644 index 0000000..df2c5eb --- /dev/null +++ b/exercises/practice/food-chain/food_chain.vader @@ -0,0 +1,60 @@ + +Execute (fly): + let g:endVerse = 1 + let g:startVerse = 1 + let g:expected = ['I know an old lady who swallowed a fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.'] + AssertEqual g:expected, Recite(g:endVerse, g:startVerse) + +Execute (spider): + let g:endVerse = 2 + let g:startVerse = 2 + let g: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.'] + AssertEqual g:expected, Recite(g:endVerse, g:startVerse) + +Execute (bird): + let g:endVerse = 3 + let g:startVerse = 3 + let g: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.'] + AssertEqual g:expected, Recite(g:endVerse, g:startVerse) + +Execute (cat): + let g:endVerse = 4 + let g:startVerse = 4 + let g: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.'] + AssertEqual g:expected, Recite(g:endVerse, g:startVerse) + +Execute (dog): + let g:endVerse = 5 + let g:startVerse = 5 + let g: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.'] + AssertEqual g:expected, Recite(g:endVerse, g:startVerse) + +Execute (goat): + let g:endVerse = 6 + let g:startVerse = 6 + let g: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.'] + AssertEqual g:expected, Recite(g:endVerse, g:startVerse) + +Execute (cow): + let g:endVerse = 7 + let g:startVerse = 7 + let g: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.'] + AssertEqual g:expected, Recite(g:endVerse, g:startVerse) + +Execute (horse): + let g:endVerse = 8 + let g:startVerse = 8 + let g:expected = ['I know an old lady who swallowed a horse.', 'She''s dead, of course!'] + AssertEqual g:expected, Recite(g:endVerse, g:startVerse) + +Execute (multiple verses): + let g:endVerse = 3 + let g:startVerse = 1 + let g: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.'] + AssertEqual g:expected, Recite(g:endVerse, g:startVerse) + +Execute (full song): + let g:endVerse = 8 + let g:startVerse = 1 + let g: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!'] + AssertEqual g:expected, Recite(g:endVerse, g:startVerse) diff --git a/exercises/practice/food-chain/food_chain.vim b/exercises/practice/food-chain/food_chain.vim new file mode 100644 index 0000000..976735f --- /dev/null +++ b/exercises/practice/food-chain/food_chain.vim @@ -0,0 +1,7 @@ +" +" Return a list containing the lyrics to `I Know an Old Lady Who Swallowed a Fly`, +" using a verse number to end on and another to start from +" +function! Recite(endVerse, startVerse) + " your solution goes here +endfunction