Skip to content

Commit 80bdad8

Browse files
Add food-chain exercise (#785)
1 parent d763db8 commit 80bdad8

File tree

10 files changed

+364
-0
lines changed

10 files changed

+364
-0
lines changed

config.json

+12
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,18 @@
12531253
"time"
12541254
]
12551255
},
1256+
{
1257+
"slug": "food-chain",
1258+
"name": "Food Chain",
1259+
"uuid": "188f12ad-e6ed-40d4-b515-d2b4d49cbb69",
1260+
"practices": [],
1261+
"prerequisites": [
1262+
"strings",
1263+
"conditionals",
1264+
"vectors"
1265+
],
1266+
"difficulty": 5
1267+
},
12561268
{
12571269
"slug": "list-ops",
12581270
"name": "List Ops",
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
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+
}
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")))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(ns food-chain-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
[clojure.string :as str]
4+
food-chain))
5+
6+
{{#test_cases.recite}}
7+
(deftest recite_test_{{idx}}
8+
(testing {{description}}
9+
(is (= (str/join "\n" [{{#expected~}}{{.}}{{#if @last}}]){{else}}
10+
{{/if}}{{/expected}}
11+
(food-chain/recite {{input.startVerse}} {{input.endVerse}})))))
12+
{{/test_cases.recite}}
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"
+6
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}}}
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"]])
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+
)
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 (= (str/join "\n" ["I know an old lady who swallowed a fly."
9+
"I don't know why she swallowed the fly. Perhaps she'll die."])
10+
(food-chain/recite 1 1)))))
11+
12+
(deftest recite_test_2
13+
(testing "spider"
14+
(is (= (str/join "\n" ["I know an old lady who swallowed a spider."
15+
"It wriggled and jiggled and tickled inside her."
16+
"She swallowed the spider to catch the fly."
17+
"I don't know why she swallowed the fly. Perhaps she'll die."])
18+
(food-chain/recite 2 2)))))
19+
20+
(deftest recite_test_3
21+
(testing "bird"
22+
(is (= (str/join "\n" ["I know an old lady who swallowed a bird."
23+
"How absurd to swallow a bird!"
24+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
25+
"She swallowed the spider to catch the fly."
26+
"I don't know why she swallowed the fly. Perhaps she'll die."])
27+
(food-chain/recite 3 3)))))
28+
29+
(deftest recite_test_4
30+
(testing "cat"
31+
(is (= (str/join "\n" ["I know an old lady who swallowed a cat."
32+
"Imagine that, to swallow a cat!"
33+
"She swallowed the cat to catch the bird."
34+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
35+
"She swallowed the spider to catch the fly."
36+
"I don't know why she swallowed the fly. Perhaps she'll die."])
37+
(food-chain/recite 4 4)))))
38+
39+
(deftest recite_test_5
40+
(testing "dog"
41+
(is (= (str/join "\n" ["I know an old lady who swallowed a dog."
42+
"What a hog, to swallow a dog!"
43+
"She swallowed the dog to catch the cat."
44+
"She swallowed the cat to catch the bird."
45+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
46+
"She swallowed the spider to catch the fly."
47+
"I don't know why she swallowed the fly. Perhaps she'll die."])
48+
(food-chain/recite 5 5)))))
49+
50+
(deftest recite_test_6
51+
(testing "goat"
52+
(is (= (str/join "\n" ["I know an old lady who swallowed a goat."
53+
"Just opened her throat and swallowed a goat!"
54+
"She swallowed the goat to catch the dog."
55+
"She swallowed the dog to catch the cat."
56+
"She swallowed the cat to catch the bird."
57+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
58+
"She swallowed the spider to catch the fly."
59+
"I don't know why she swallowed the fly. Perhaps she'll die."])
60+
(food-chain/recite 6 6)))))
61+
62+
(deftest recite_test_7
63+
(testing "cow"
64+
(is (= (str/join "\n" ["I know an old lady who swallowed a cow."
65+
"I don't know how she swallowed a cow!"
66+
"She swallowed the cow to catch the goat."
67+
"She swallowed the goat to catch the dog."
68+
"She swallowed the dog to catch the cat."
69+
"She swallowed the cat to catch the bird."
70+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
71+
"She swallowed the spider to catch the fly."
72+
"I don't know why she swallowed the fly. Perhaps she'll die."])
73+
(food-chain/recite 7 7)))))
74+
75+
(deftest recite_test_8
76+
(testing "horse"
77+
(is (= (str/join "\n" ["I know an old lady who swallowed a horse."
78+
"She's dead, of course!"])
79+
(food-chain/recite 8 8)))))
80+
81+
(deftest recite_test_9
82+
(testing "multiple verses"
83+
(is (= (str/join "\n" ["I know an old lady who swallowed a fly."
84+
"I don't know why she swallowed the fly. Perhaps she'll die."
85+
""
86+
"I know an old lady who swallowed a spider."
87+
"It wriggled and jiggled and tickled inside her."
88+
"She swallowed the spider to catch the fly."
89+
"I don't know why she swallowed the fly. Perhaps she'll die."
90+
""
91+
"I know an old lady who swallowed a bird."
92+
"How absurd to swallow a bird!"
93+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
94+
"She swallowed the spider to catch the fly."
95+
"I don't know why she swallowed the fly. Perhaps she'll die."])
96+
(food-chain/recite 1 3)))))
97+
98+
(deftest recite_test_10
99+
(testing "full song"
100+
(is (= (str/join "\n" ["I know an old lady who swallowed a fly."
101+
"I don't know why she swallowed the fly. Perhaps she'll die."
102+
""
103+
"I know an old lady who swallowed a spider."
104+
"It wriggled and jiggled and tickled inside her."
105+
"She swallowed the spider to catch the fly."
106+
"I don't know why she swallowed the fly. Perhaps she'll die."
107+
""
108+
"I know an old lady who swallowed a bird."
109+
"How absurd to swallow a bird!"
110+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
111+
"She swallowed the spider to catch the fly."
112+
"I don't know why she swallowed the fly. Perhaps she'll die."
113+
""
114+
"I know an old lady who swallowed a cat."
115+
"Imagine that, to swallow a cat!"
116+
"She swallowed the cat to catch the bird."
117+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
118+
"She swallowed the spider to catch the fly."
119+
"I don't know why she swallowed the fly. Perhaps she'll die."
120+
""
121+
"I know an old lady who swallowed a dog."
122+
"What a hog, to swallow a dog!"
123+
"She swallowed the dog to catch the cat."
124+
"She swallowed the cat to catch the bird."
125+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
126+
"She swallowed the spider to catch the fly."
127+
"I don't know why she swallowed the fly. Perhaps she'll die."
128+
""
129+
"I know an old lady who swallowed a goat."
130+
"Just opened her throat and swallowed a goat!"
131+
"She swallowed the goat to catch the dog."
132+
"She swallowed the dog to catch the cat."
133+
"She swallowed the cat to catch the bird."
134+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
135+
"She swallowed the spider to catch the fly."
136+
"I don't know why she swallowed the fly. Perhaps she'll die."
137+
""
138+
"I know an old lady who swallowed a cow."
139+
"I don't know how she swallowed a cow!"
140+
"She swallowed the cow to catch the goat."
141+
"She swallowed the goat to catch the dog."
142+
"She swallowed the dog to catch the cat."
143+
"She swallowed the cat to catch the bird."
144+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
145+
"She swallowed the spider to catch the fly."
146+
"I don't know why she swallowed the fly. Perhaps she'll die."
147+
""
148+
"I know an old lady who swallowed a horse."
149+
"She's dead, of course!"])
150+
(food-chain/recite 1 8)))))

0 commit comments

Comments
 (0)