Skip to content

Commit

Permalink
Implement exercise 'food-chain'
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Oct 3, 2024
1 parent c04c6b4 commit 6b19b75
Show file tree
Hide file tree
Showing 15 changed files with 736 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "food-chain",
"name": "Food Chain",
"uuid": "fb4bd2e9-9ca0-41e3-90c7-1adad92d0dd1",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "matching-brackets",
"name": "Matching Brackets",
Expand Down
7 changes: 7 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ exercises:
prerequisites: []
difficulty: 5

- slug: food-chain
name: Food Chain
uuid: fb4bd2e9-9ca0-41e3-90c7-1adad92d0dd1
practices: []
prerequisites: []
difficulty: 5

- slug: matching-brackets
name: Matching Brackets
uuid: a6c79db6-925c-45c3-8a59-aa9d96a4740b
Expand Down
64 changes: 64 additions & 0 deletions exercises/practice/food-chain/.docs/instructions.md
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YS_VERSION := 0.1.75
28 changes: 28 additions & 0 deletions exercises/practice/food-chain/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.79

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

YS_LOCAL_BIN := $(YS_LOCAL_PREFIX)/bin

YS_BIN := $(YS_LOCAL_BIN)/ys-$(YS_VERSION)

TEST_FILE ?= $(wildcard *-test.ys)


export PATH := $(YS_LOCAL_BIN):$(PATH)

export YSPATH := $(BASE)


default:

test: $(YS_BIN)
prove -v $(TEST_FILE)

$(YS_BIN):
curl -s https://yamlscript.org/install | \
BIN=1 VERSION=$(YS_VERSION) PREFIX=$(YS_LOCAL_PREFIX) bash >/dev/null
23 changes: 23 additions & 0 deletions exercises/practice/food-chain/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"ingydotnet"
],
"files": {
"solution": [
"food-chain.ys"
],
"test": [
"food-chain-test.ys",
"GNUmakefile",
"Makefile",
".yamlscript/exercise.mk",
".yamlscript/exercism-ys-installer"
],
"example": [
".meta/food-chain.ys"
]
},
"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"
}
173 changes: 173 additions & 0 deletions exercises/practice/food-chain/.meta/food-chain-test.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/usr/bin/env ys-0

require ys::taptest: :all

use: food-chain

# JSON doesn't allow for multi-line strings, so all verses are presented
# here as arrays of strings. It's up to the test generator to join the
# lines together with line breaks.
# Some languages test for the verse() method, which takes a start verse
# and optional end verse, but other languages have only tested for the full poem.
# For those languages in the latter category, you may wish to only
# implement the full song test and leave the rest alone, ignoring the start
# and end verse fields.

test::
- name: Fly
code: recite(1 1)
want:
- I know an old lady who swallowed a fly.
- I don't know why she swallowed the fly. Perhaps she'll die.
uuid: 751dce68-9412-496e-b6e8-855998c56166

- name: Spider
code: recite(2 2)
want:
- 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.
uuid: 6c56f861-0c5e-4907-9a9d-b2efae389379

- name: Bird
code: recite(3 3)
want:
- 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.
uuid: 3edf5f33-bef1-4e39-ae67-ca5eb79203fa

- name: Cat
code: recite(4 4)
want:
- 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.
uuid: e866a758-e1ff-400e-9f35-f27f28cc288f

- name: Dog
code: recite(5 5)
want:
- 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.
uuid: 3f02c30e-496b-4b2a-8491-bc7e2953cafb

- name: Goat
code: recite(6 6)
want:
- 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.
uuid: 4b3fd221-01ea-46e0-825b-5734634fbc59

- name: Cow
code: recite(7 7)
want:
- 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.
uuid: 1b707da9-7001-4fac-941f-22ad9c7a65d4

- name: Horse
code: recite(8 8)
want:
- I know an old lady who swallowed a horse.
- She's dead, of course!
uuid: 3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc

- name: Multiple verses
code: recite(1 3)
want:
- 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.
uuid: 22b863d5-17e4-4d1e-93e4-617329a5c050

- name: Full song
code: recite(1 8)
want:
- 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!
uuid: e626b32b-745c-4101-bcbd-3b13456893db

done: 10
40 changes: 40 additions & 0 deletions exercises/practice/food-chain/.meta/food-chain.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
!yamlscript/v0

barn =::
- 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!

defn recite(start-verse end-verse):
drop-last:
reduce: add-verse [] (start-verse.-- .. end-verse.--)

defn add-verse(lines verse):
animal =: barn.$verse.first().0
lines =: conj(lines "I know an old lady who swallowed a $animal.")
lines =: conj(lines barn.$verse.first().1)
lines =:
if 0 < verse < barn.#.--:
conj _ barn.0:first.1:
reduce _ lines (verse .. 0):
fn(lines verse):
lines =:
if verse.?:
then:
animal1 animal2 =:
L(barn.$verse.first().0 barn.nth(verse.--):first.0)
line =: "She swallowed the $animal1 to catch the $animal2."
line =:
if animal2 == 'spider':
replace(line /\./ replace(barn.1:first.1 /It/ ' that'))
line
conj lines: line
else: lines
=>: lines
=>: lines
conj lines: ''
Loading

0 comments on commit 6b19b75

Please sign in to comment.