Skip to content

Commit

Permalink
Add food chain (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakelewis authored Mar 8, 2024
1 parent a9a3c7e commit 5fb1562
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "food-chain",
"name": "Food Chain",
"uuid": "fed848ba-5a1d-4b6b-a145-e6d77e40be4d",
"practices": [],
"prerequisites": [],
"difficulty": 2
}
]
},
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
17 changes: 17 additions & 0 deletions exercises/practice/food-chain/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["blakelewis"],
"files": {
"solution": [
"food-chain.rkt"
],
"test": [
"food-chain-test.rkt"
],
"example": [
".meta/example.rkt"
]
},
"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"
}
52 changes: 52 additions & 0 deletions exercises/practice/food-chain/.meta/example.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#lang racket

(provide recite)

(define spider-action
" wriggled and jiggled and tickled inside her")

(define verdict
"I don't know why she swallowed the fly. Perhaps she'll die.")

(struct critter (name reaction extra))

(define critters
(list
(critter "horse" "She's dead, of course!" "")
(critter "cow" "I don't know how she swallowed a cow!" "")
(critter "goat" "Just opened her throat and swallowed a goat!" "")
(critter "dog" "What a hog, to swallow a dog!" "")
(critter "cat" "Imagine that, to swallow a cat!" "")
(critter "bird" "How absurd to swallow a bird!" "")
(critter "spider" (string-append "It" spider-action ".")
(string-append " that" spider-action))
(critter "fly" verdict "")))

(define (intro c)
(list
(string-append "I know an old lady who swallowed a "
(critter-name c) ".")
(critter-reaction c)))

(define (reason previous c)
(string-append "She swallowed the " previous " to catch the "
(critter-name c) (critter-extra c) "."))

(define (verse n)
(let* ([cs (drop critters (- (length critters) n))]
[start (intro (car cs))])
(if (or (eq? cs critters) (null? (cdr cs)))
start
(for/fold
([v (reverse start)]
[previous (critter-name (car cs))]
#:result (reverse (cons verdict v)))
([c (cdr cs)])
(values (cons (reason previous c) v) (critter-name c))))))

(define (recite start-verse end-verse)
(for/fold
([song '()])
([i (in-inclusive-range start-verse end-verse)])
(let ([v (if (= i start-verse) (verse i) (cons "" (verse i)))])
(append song v))))
40 changes: 40 additions & 0 deletions exercises/practice/food-chain/.meta/tests.toml
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"
148 changes: 148 additions & 0 deletions exercises/practice/food-chain/food-chain-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#lang racket/base

(require "food-chain.rkt")

(module+ test
(require rackunit rackunit/text-ui))

(module+ test
(define suite
(test-suite "food chain tests"

(test-equal? "fly"
(recite 1 1)
'("I know an old lady who swallowed a fly."
"I don't know why she swallowed the fly. Perhaps she'll die."))

(test-equal? "spider"
(recite 2 2)
'("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."))

(test-equal? "bird"
(recite 3 3)
'("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."))

(test-equal? "cat"
(recite 4 4)
'("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."))

(test-equal? "dog"
(recite 5 5)
'("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."))

(test-equal? "goat"
(recite 6 6)
'("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."))

(test-equal? "cow"
(recite 7 7)
'("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."))

(test-equal? "horse"
(recite 8 8)
'("I know an old lady who swallowed a horse."
"She's dead, of course!"))

(test-equal? "multiple verses"
(recite 1 3)
'("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."))

(test-equal? "full song"
(recite 1 8)
'("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!"))))

(run-tests suite))
6 changes: 6 additions & 0 deletions exercises/practice/food-chain/food-chain.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#lang racket

(provide recite)

(define (recite start-verse end-verse)
(error "Not implemented yet"))

0 comments on commit 5fb1562

Please sign in to comment.