Skip to content

Commit 1521031

Browse files
Add food-chain exercise
1 parent ac40704 commit 1521031

10 files changed

Lines changed: 359 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,14 @@
12121212
],
12131213
"difficulty": 4
12141214
},
1215+
{
1216+
"slug": "food-chain",
1217+
"name": "Food Chain",
1218+
"uuid": "188f12ad-e6ed-40d4-b515-d2b4d49cbb69",
1219+
"practices": [],
1220+
"prerequisites": [],
1221+
"difficulty": 4
1222+
},
12151223
{
12161224
"slug": "spiral-matrix",
12171225
"name": "Spiral Matrix",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Instructions
2+
3+
Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.
4+
5+
While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.
6+
7+
This is a [cumulative song][cumulative-song] of unknown origin.
8+
9+
This is one of many common variants.
10+
11+
```text
12+
I know an old lady who swallowed a fly.
13+
I don't know why she swallowed the fly. Perhaps she'll die.
14+
15+
I know an old lady who swallowed a spider.
16+
It wriggled and jiggled and tickled inside her.
17+
She swallowed the spider to catch the fly.
18+
I don't know why she swallowed the fly. Perhaps she'll die.
19+
20+
I know an old lady who swallowed a bird.
21+
How absurd to swallow a bird!
22+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
23+
She swallowed the spider to catch the fly.
24+
I don't know why she swallowed the fly. Perhaps she'll die.
25+
26+
I know an old lady who swallowed a cat.
27+
Imagine that, to swallow a cat!
28+
She swallowed the cat to catch the bird.
29+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
30+
She swallowed the spider to catch the fly.
31+
I don't know why she swallowed the fly. Perhaps she'll die.
32+
33+
I know an old lady who swallowed a dog.
34+
What a hog, to swallow a dog!
35+
She swallowed the dog to catch the cat.
36+
She swallowed the cat to catch the bird.
37+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
38+
She swallowed the spider to catch the fly.
39+
I don't know why she swallowed the fly. Perhaps she'll die.
40+
41+
I know an old lady who swallowed a goat.
42+
Just opened her throat and swallowed a goat!
43+
She swallowed the goat to catch the dog.
44+
She swallowed the dog to catch the cat.
45+
She swallowed the cat to catch the bird.
46+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
47+
She swallowed the spider to catch the fly.
48+
I don't know why she swallowed the fly. Perhaps she'll die.
49+
50+
I know an old lady who swallowed a cow.
51+
I don't know how she swallowed a cow!
52+
She swallowed the cow to catch the goat.
53+
She swallowed the goat to catch the dog.
54+
She swallowed the dog to catch the cat.
55+
She swallowed the cat to catch the bird.
56+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
57+
She swallowed the spider to catch the fly.
58+
I don't know why she swallowed the fly. Perhaps she'll die.
59+
60+
I know an old lady who swallowed a horse.
61+
She's dead, of course!
62+
```
63+
64+
[cumulative-song]: https://en.wikipedia.org/wiki/Cumulative_song
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"erikschierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/food_chain.clj"
8+
],
9+
"test": [
10+
"test/food_chain_test.clj"
11+
],
12+
"example": [
13+
".meta/example.clj"
14+
]
15+
},
16+
"blurb": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly"
19+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(ns food-chain
2+
(:require [clojure.string :as str]))
3+
4+
(def animals ["fly" "spider" "bird" "cat" "dog" "goat" "cow" "horse"])
5+
6+
(def phrases [nil
7+
"It wriggled and jiggled and tickled inside her."
8+
"How absurd to swallow a bird!"
9+
"Imagine that, to swallow a cat!"
10+
"What a hog, to swallow a dog!"
11+
"Just opened her throat and swallowed a goat!"
12+
"I don't know how she swallowed a cow!"
13+
nil])
14+
15+
(defn verse-begin [n]
16+
(let [first (str "I know an old lady who swallowed a " (get animals n) ".")
17+
second (get phrases n)]
18+
(remove nil? [first second])))
19+
20+
(defn verse-caught [caught]
21+
(if (= "spider" caught)
22+
(str caught " that wriggled and jiggled and tickled inside her")
23+
caught))
24+
25+
(defn verse-swallow [[swallowed caught]]
26+
(str "She swallowed the " swallowed " to catch the " (verse-caught caught) "."))
27+
28+
(defn verse-swallows [n]
29+
(mapv verse-swallow (partition 2 1 (reverse (take (inc n) animals)))))
30+
31+
(defn verse-middle [n]
32+
(if (< 0 n 7)
33+
(verse-swallows n)
34+
[]))
35+
36+
(defn verse-end [n]
37+
(if (< n 7)
38+
"I don't know why she swallowed the fly. Perhaps she'll die."
39+
"She's dead, of course!"))
40+
41+
(defn verse [n]
42+
(->> (cons (verse-begin n) (conj (verse-middle n) (verse-end n)))
43+
(flatten)
44+
(str/join "\n")))
45+
46+
(defn recite [start-verse end-verse]
47+
(->> (range (dec start-verse) end-verse)
48+
(map verse)
49+
(interpose "")
50+
(str/join "\n")))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(ns food-chain-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
[clojure.string :as str]
4+
food-chain))
5+
{{#test_cases.recite}}
6+
(deftest recite_test_{{idx}}
7+
(testing {{description}}
8+
(is (= (food-chain/recite {{input.startVerse}} {{input.endVerse}})
9+
(str/join "\n" [{{#expected}}{{.}}{{#if @last}}]))))){{/if}}
10+
{{/expected~}}
11+
{{/test_cases.recite~}}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[751dce68-9412-496e-b6e8-855998c56166]
13+
description = "fly"
14+
15+
[6c56f861-0c5e-4907-9a9d-b2efae389379]
16+
description = "spider"
17+
18+
[3edf5f33-bef1-4e39-ae67-ca5eb79203fa]
19+
description = "bird"
20+
21+
[e866a758-e1ff-400e-9f35-f27f28cc288f]
22+
description = "cat"
23+
24+
[3f02c30e-496b-4b2a-8491-bc7e2953cafb]
25+
description = "dog"
26+
27+
[4b3fd221-01ea-46e0-825b-5734634fbc59]
28+
description = "goat"
29+
30+
[1b707da9-7001-4fac-941f-22ad9c7a65d4]
31+
description = "cow"
32+
33+
[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc]
34+
description = "horse"
35+
36+
[22b863d5-17e4-4d1e-93e4-617329a5c050]
37+
description = "multiple verses"
38+
39+
[e626b32b-745c-4101-bcbd-3b13456893db]
40+
description = "full song"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{:aliases {:test {:extra-paths ["test"]
2+
:extra-deps {io.github.cognitect-labs/test-runner
3+
{:git/url "https://github.com/cognitect-labs/test-runner.git"
4+
:sha "705ad25bbf0228b1c38d0244a36001c2987d7337"}}
5+
:main-opts ["-m" "cognitect.test-runner"]
6+
:exec-fn cognitect.test-runner.api/test}}}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(defproject food-chain "0.1.0-SNAPSHOT"
2+
:description "food-chain exercise."
3+
:url "https://github.com/exercism/clojure/tree/main/exercises/practice/food-chain"
4+
:dependencies [[org.clojure/clojure "1.11.1"]])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(ns food-chain)
2+
3+
(defn recite
4+
"Returns the lyrics of the song: 'I Know an Old Lady Who Swallowed a Fly.'"
5+
[start-verse end-verse]
6+
;; function body
7+
)
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
(ns food-chain-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
[clojure.string :as str]
4+
food-chain))
5+
6+
(deftest recite_test_1
7+
(testing "fly"
8+
(is (= (food-chain/recite 1 1)
9+
(str/join "\n" ["I know an old lady who swallowed a fly."
10+
"I don't know why she swallowed the fly. Perhaps she'll die."])))))
11+
12+
(deftest recite_test_2
13+
(testing "spider"
14+
(is (= (food-chain/recite 2 2)
15+
(str/join "\n" ["I know an old lady who swallowed a spider."
16+
"It wriggled and jiggled and tickled inside her."
17+
"She swallowed the spider to catch the fly."
18+
"I don't know why she swallowed the fly. Perhaps she'll die."])))))
19+
20+
(deftest recite_test_3
21+
(testing "bird"
22+
(is (= (food-chain/recite 3 3)
23+
(str/join "\n" ["I know an old lady who swallowed a bird."
24+
"How absurd to swallow a bird!"
25+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
26+
"She swallowed the spider to catch the fly."
27+
"I don't know why she swallowed the fly. Perhaps she'll die."])))))
28+
29+
(deftest recite_test_4
30+
(testing "cat"
31+
(is (= (food-chain/recite 4 4)
32+
(str/join "\n" ["I know an old lady who swallowed a cat."
33+
"Imagine that, to swallow a cat!"
34+
"She swallowed the cat to catch the bird."
35+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
36+
"She swallowed the spider to catch the fly."
37+
"I don't know why she swallowed the fly. Perhaps she'll die."])))))
38+
39+
(deftest recite_test_5
40+
(testing "dog"
41+
(is (= (food-chain/recite 5 5)
42+
(str/join "\n" ["I know an old lady who swallowed a dog."
43+
"What a hog, to swallow a dog!"
44+
"She swallowed the dog to catch the cat."
45+
"She swallowed the cat to catch the bird."
46+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
47+
"She swallowed the spider to catch the fly."
48+
"I don't know why she swallowed the fly. Perhaps she'll die."])))))
49+
50+
(deftest recite_test_6
51+
(testing "goat"
52+
(is (= (food-chain/recite 6 6)
53+
(str/join "\n" ["I know an old lady who swallowed a goat."
54+
"Just opened her throat and swallowed a goat!"
55+
"She swallowed the goat to catch the dog."
56+
"She swallowed the dog to catch the cat."
57+
"She swallowed the cat to catch the bird."
58+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
59+
"She swallowed the spider to catch the fly."
60+
"I don't know why she swallowed the fly. Perhaps she'll die."])))))
61+
62+
(deftest recite_test_7
63+
(testing "cow"
64+
(is (= (food-chain/recite 7 7)
65+
(str/join "\n" ["I know an old lady who swallowed a cow."
66+
"I don't know how she swallowed a cow!"
67+
"She swallowed the cow to catch the goat."
68+
"She swallowed the goat to catch the dog."
69+
"She swallowed the dog to catch the cat."
70+
"She swallowed the cat to catch the bird."
71+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
72+
"She swallowed the spider to catch the fly."
73+
"I don't know why she swallowed the fly. Perhaps she'll die."])))))
74+
75+
(deftest recite_test_8
76+
(testing "horse"
77+
(is (= (food-chain/recite 8 8)
78+
(str/join "\n" ["I know an old lady who swallowed a horse."
79+
"She's dead, of course!"])))))
80+
81+
(deftest recite_test_9
82+
(testing "multiple verses"
83+
(is (= (food-chain/recite 1 3)
84+
(str/join "\n" ["I know an old lady who swallowed a fly."
85+
"I don't know why she swallowed the fly. Perhaps she'll die."
86+
""
87+
"I know an old lady who swallowed a spider."
88+
"It wriggled and jiggled and tickled inside her."
89+
"She swallowed the spider to catch the fly."
90+
"I don't know why she swallowed the fly. Perhaps she'll die."
91+
""
92+
"I know an old lady who swallowed a bird."
93+
"How absurd to swallow a bird!"
94+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
95+
"She swallowed the spider to catch the fly."
96+
"I don't know why she swallowed the fly. Perhaps she'll die."])))))
97+
98+
(deftest recite_test_10
99+
(testing "full song"
100+
(is (= (food-chain/recite 1 8)
101+
(str/join "\n" ["I know an old lady who swallowed a fly."
102+
"I don't know why she swallowed the fly. Perhaps she'll die."
103+
""
104+
"I know an old lady who swallowed a spider."
105+
"It wriggled and jiggled and tickled inside her."
106+
"She swallowed the spider to catch the fly."
107+
"I don't know why she swallowed the fly. Perhaps she'll die."
108+
""
109+
"I know an old lady who swallowed a bird."
110+
"How absurd to swallow a bird!"
111+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
112+
"She swallowed the spider to catch the fly."
113+
"I don't know why she swallowed the fly. Perhaps she'll die."
114+
""
115+
"I know an old lady who swallowed a cat."
116+
"Imagine that, to swallow a cat!"
117+
"She swallowed the cat to catch the bird."
118+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
119+
"She swallowed the spider to catch the fly."
120+
"I don't know why she swallowed the fly. Perhaps she'll die."
121+
""
122+
"I know an old lady who swallowed a dog."
123+
"What a hog, to swallow a dog!"
124+
"She swallowed the dog to catch the cat."
125+
"She swallowed the cat to catch the bird."
126+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
127+
"She swallowed the spider to catch the fly."
128+
"I don't know why she swallowed the fly. Perhaps she'll die."
129+
""
130+
"I know an old lady who swallowed a goat."
131+
"Just opened her throat and swallowed a goat!"
132+
"She swallowed the goat to catch the dog."
133+
"She swallowed the dog to catch the cat."
134+
"She swallowed the cat to catch the bird."
135+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
136+
"She swallowed the spider to catch the fly."
137+
"I don't know why she swallowed the fly. Perhaps she'll die."
138+
""
139+
"I know an old lady who swallowed a cow."
140+
"I don't know how she swallowed a cow!"
141+
"She swallowed the cow to catch the goat."
142+
"She swallowed the goat to catch the dog."
143+
"She swallowed the dog to catch the cat."
144+
"She swallowed the cat to catch the bird."
145+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
146+
"She swallowed the spider to catch the fly."
147+
"I don't know why she swallowed the fly. Perhaps she'll die."
148+
""
149+
"I know an old lady who swallowed a horse."
150+
"She's dead, of course!"])))))

0 commit comments

Comments
 (0)