Skip to content

Commit 1e6859a

Browse files
Add twelve-days exercise (#771)
1 parent 82354ea commit 1e6859a

10 files changed

Lines changed: 315 additions & 0 deletions

File tree

config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,18 @@
11781178
"math"
11791179
]
11801180
},
1181+
{
1182+
"slug": "twelve-days",
1183+
"name": "Twelve Days",
1184+
"uuid": "dc3b4248-6e77-46ec-b182-52d1727b3071",
1185+
"practices": [],
1186+
"prerequisites": [
1187+
"strings",
1188+
"conditionals",
1189+
"vectors"
1190+
],
1191+
"difficulty": 4
1192+
},
11811193
{
11821194
"slug": "spiral-matrix",
11831195
"name": "Spiral Matrix",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Instructions
2+
3+
Your task in this exercise is to write code that returns the lyrics of the song: "The Twelve Days of Christmas."
4+
5+
"The Twelve Days of Christmas" is a common English Christmas carol.
6+
Each subsequent verse of the song builds on the previous verse.
7+
8+
The lyrics your code returns should _exactly_ match the full song text shown below.
9+
10+
## Lyrics
11+
12+
```text
13+
On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.
14+
15+
On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.
16+
17+
On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
18+
19+
On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
20+
21+
On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
22+
23+
On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
24+
25+
On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
26+
27+
On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
28+
29+
On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
30+
31+
On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
32+
33+
On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
34+
35+
On the twelfth day of Christmas my true love gave to me: twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
36+
```
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/twelve_days.clj"
8+
],
9+
"test": [
10+
"test/twelve_days_test.clj"
11+
],
12+
"example": [
13+
".meta/example.clj"
14+
]
15+
},
16+
"blurb": "Output the lyrics to 'The Twelve Days of Christmas'.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_(song)"
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
(ns twelve-days
2+
(:require [clojure.string :as str]))
3+
4+
(def gifts ["a Partridge in a Pear Tree"
5+
"two Turtle Doves"
6+
"three French Hens"
7+
"four Calling Birds"
8+
"five Gold Rings"
9+
"six Geese-a-Laying"
10+
"seven Swans-a-Swimming"
11+
"eight Maids-a-Milking"
12+
"nine Ladies Dancing"
13+
"ten Lords-a-Leaping"
14+
"eleven Pipers Piping"
15+
"twelve Drummers Drumming"])
16+
17+
(def days ["first"
18+
"second"
19+
"third"
20+
"fourth"
21+
"fifth"
22+
"sixth"
23+
"seventh"
24+
"eighth"
25+
"ninth"
26+
"tenth"
27+
"eleventh"
28+
"twelfth"])
29+
30+
(defn line-gifts [n]
31+
(->> gifts
32+
(take n)
33+
(reverse)
34+
(str/join ", ")
35+
(#(str/replace % #", a" ", and a"))))
36+
37+
(defn line [n]
38+
(str "On the " (nth days (dec n)) " day of Christmas my true love gave to me: " (line-gifts n) "."))
39+
40+
(defn recite [start-verse end-verse]
41+
(map line (range start-verse (inc end-verse))))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(ns twelve-days-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
twelve-days))
4+
{{#test_cases.recite}}
5+
(deftest recite_test_{{idx}}
6+
(testing {{description}}
7+
(is (= (twelve-days/recite {{input.startVerse}} {{input.endVerse}})
8+
[{{~#expected}}
9+
{{.}}
10+
{{~/expected}}
11+
]))))
12+
{{/test_cases.recite~}}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
[c0b5a5e6-c89d-49b1-a6b2-9f523bff33f7]
13+
description = "verse -> first day a partridge in a pear tree"
14+
15+
[1c64508a-df3d-420a-b8e1-fe408847854a]
16+
description = "verse -> second day two turtle doves"
17+
18+
[a919e09c-75b2-4e64-bb23-de4a692060a8]
19+
description = "verse -> third day three french hens"
20+
21+
[9bed8631-ec60-4894-a3bb-4f0ec9fbe68d]
22+
description = "verse -> fourth day four calling birds"
23+
24+
[cf1024f0-73b6-4545-be57-e9cea565289a]
25+
description = "verse -> fifth day five gold rings"
26+
27+
[50bd3393-868a-4f24-a618-68df3d02ff04]
28+
description = "verse -> sixth day six geese-a-laying"
29+
30+
[8f29638c-9bf1-4680-94be-e8b84e4ade83]
31+
description = "verse -> seventh day seven swans-a-swimming"
32+
33+
[7038d6e1-e377-47ad-8c37-10670a05bc05]
34+
description = "verse -> eighth day eight maids-a-milking"
35+
36+
[37a800a6-7a56-4352-8d72-0f51eb37cfe8]
37+
description = "verse -> ninth day nine ladies dancing"
38+
39+
[10b158aa-49ff-4b2d-afc3-13af9133510d]
40+
description = "verse -> tenth day ten lords-a-leaping"
41+
42+
[08d7d453-f2ba-478d-8df0-d39ea6a4f457]
43+
description = "verse -> eleventh day eleven pipers piping"
44+
45+
[0620fea7-1704-4e48-b557-c05bf43967f0]
46+
description = "verse -> twelfth day twelve drummers drumming"
47+
48+
[da8b9013-b1e8-49df-b6ef-ddec0219e398]
49+
description = "lyrics -> recites first three verses of the song"
50+
51+
[c095af0d-3137-4653-ad32-bfb899eda24c]
52+
description = "lyrics -> recites three verses from the middle of the song"
53+
54+
[20921bc9-cc52-4627-80b3-198cbbfcf9b7]
55+
description = "lyrics -> recites the whole 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 twelve-days "0.1.0-SNAPSHOT"
2+
:description "twelve-days exercise."
3+
:url "https://github.com/exercism/clojure/tree/main/exercises/practice/twelve-days"
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 twelve-days)
2+
3+
(defn recite
4+
"Returns the lyrics of the song: 'The Twelve Days of Christmas.'"
5+
[start-verse end-verse]
6+
;; function body
7+
)
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
(ns twelve-days-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
twelve-days))
4+
5+
(deftest recite_test_1
6+
(testing "verse - first day a partridge in a pear tree"
7+
(is (= (twelve-days/recite 1 1)
8+
[
9+
"On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree."
10+
]))))
11+
12+
(deftest recite_test_2
13+
(testing "verse - second day two turtle doves"
14+
(is (= (twelve-days/recite 2 2)
15+
[
16+
"On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree."
17+
]))))
18+
19+
(deftest recite_test_3
20+
(testing "verse - third day three french hens"
21+
(is (= (twelve-days/recite 3 3)
22+
[
23+
"On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
24+
]))))
25+
26+
(deftest recite_test_4
27+
(testing "verse - fourth day four calling birds"
28+
(is (= (twelve-days/recite 4 4)
29+
[
30+
"On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
31+
]))))
32+
33+
(deftest recite_test_5
34+
(testing "verse - fifth day five gold rings"
35+
(is (= (twelve-days/recite 5 5)
36+
[
37+
"On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
38+
]))))
39+
40+
(deftest recite_test_6
41+
(testing "verse - sixth day six geese-a-laying"
42+
(is (= (twelve-days/recite 6 6)
43+
[
44+
"On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
45+
]))))
46+
47+
(deftest recite_test_7
48+
(testing "verse - seventh day seven swans-a-swimming"
49+
(is (= (twelve-days/recite 7 7)
50+
[
51+
"On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
52+
]))))
53+
54+
(deftest recite_test_8
55+
(testing "verse - eighth day eight maids-a-milking"
56+
(is (= (twelve-days/recite 8 8)
57+
[
58+
"On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
59+
]))))
60+
61+
(deftest recite_test_9
62+
(testing "verse - ninth day nine ladies dancing"
63+
(is (= (twelve-days/recite 9 9)
64+
[
65+
"On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
66+
]))))
67+
68+
(deftest recite_test_10
69+
(testing "verse - tenth day ten lords-a-leaping"
70+
(is (= (twelve-days/recite 10 10)
71+
[
72+
"On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
73+
]))))
74+
75+
(deftest recite_test_11
76+
(testing "verse - eleventh day eleven pipers piping"
77+
(is (= (twelve-days/recite 11 11)
78+
[
79+
"On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
80+
]))))
81+
82+
(deftest recite_test_12
83+
(testing "verse - twelfth day twelve drummers drumming"
84+
(is (= (twelve-days/recite 12 12)
85+
[
86+
"On the twelfth day of Christmas my true love gave to me: twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
87+
]))))
88+
89+
(deftest recite_test_13
90+
(testing "lyrics - recites first three verses of the song"
91+
(is (= (twelve-days/recite 1 3)
92+
[
93+
"On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree."
94+
"On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree."
95+
"On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
96+
]))))
97+
98+
(deftest recite_test_14
99+
(testing "lyrics - recites three verses from the middle of the song"
100+
(is (= (twelve-days/recite 4 6)
101+
[
102+
"On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
103+
"On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
104+
"On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
105+
]))))
106+
107+
(deftest recite_test_15
108+
(testing "lyrics - recites the whole song"
109+
(is (= (twelve-days/recite 1 12)
110+
[
111+
"On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree."
112+
"On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree."
113+
"On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
114+
"On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
115+
"On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
116+
"On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
117+
"On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
118+
"On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
119+
"On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
120+
"On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
121+
"On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
122+
"On the twelfth day of Christmas my true love gave to me: twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
123+
]))))

0 commit comments

Comments
 (0)