Skip to content

Commit 9ee5a73

Browse files
authored
Bottle song (#857)
1 parent 81dbf95 commit 9ee5a73

File tree

9 files changed

+273
-0
lines changed

9 files changed

+273
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,14 @@
12821282
"practices": [],
12831283
"prerequisites": [],
12841284
"difficulty": 3
1285+
},
1286+
{
1287+
"slug": "bottle-song",
1288+
"name": "Bottle Song",
1289+
"uuid": "0c786feb-8653-4215-9281-2e1c83afeefe",
1290+
"practices": [],
1291+
"prerequisites": [],
1292+
"difficulty": 3
12851293
}
12861294
]
12871295
},
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Instructions
2+
3+
Recite the lyrics to that popular children's repetitive song: Ten Green Bottles.
4+
5+
Note that not all verses are identical.
6+
7+
```text
8+
Ten green bottles hanging on the wall,
9+
Ten green bottles hanging on the wall,
10+
And if one green bottle should accidentally fall,
11+
There'll be nine green bottles hanging on the wall.
12+
13+
Nine green bottles hanging on the wall,
14+
Nine green bottles hanging on the wall,
15+
And if one green bottle should accidentally fall,
16+
There'll be eight green bottles hanging on the wall.
17+
18+
Eight green bottles hanging on the wall,
19+
Eight green bottles hanging on the wall,
20+
And if one green bottle should accidentally fall,
21+
There'll be seven green bottles hanging on the wall.
22+
23+
Seven green bottles hanging on the wall,
24+
Seven green bottles hanging on the wall,
25+
And if one green bottle should accidentally fall,
26+
There'll be six green bottles hanging on the wall.
27+
28+
Six green bottles hanging on the wall,
29+
Six green bottles hanging on the wall,
30+
And if one green bottle should accidentally fall,
31+
There'll be five green bottles hanging on the wall.
32+
33+
Five green bottles hanging on the wall,
34+
Five green bottles hanging on the wall,
35+
And if one green bottle should accidentally fall,
36+
There'll be four green bottles hanging on the wall.
37+
38+
Four green bottles hanging on the wall,
39+
Four green bottles hanging on the wall,
40+
And if one green bottle should accidentally fall,
41+
There'll be three green bottles hanging on the wall.
42+
43+
Three green bottles hanging on the wall,
44+
Three green bottles hanging on the wall,
45+
And if one green bottle should accidentally fall,
46+
There'll be two green bottles hanging on the wall.
47+
48+
Two green bottles hanging on the wall,
49+
Two green bottles hanging on the wall,
50+
And if one green bottle should accidentally fall,
51+
There'll be one green bottle hanging on the wall.
52+
53+
One green bottle hanging on the wall,
54+
One green bottle hanging on the wall,
55+
And if one green bottle should accidentally fall,
56+
There'll be no green bottles hanging on the wall.
57+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object BottleSong:
2+
private val numbers = List("no", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten")
3+
4+
private def suffix(bottles: Int) = if bottles == 1 then "" else "s"
5+
6+
private def hanging(bottles: Int) =
7+
s"${numbers(bottles)} green bottle${suffix(bottles)} hanging on the wall"
8+
9+
private def firstLine(bottles: Int) = hanging(bottles) + ",\n"
10+
11+
private def lastLine(bottles: Int) = "There'll be " + hanging(bottles).toLowerCase + ".\n"
12+
13+
private def verse(bottles: Int) = firstLine(bottles).repeat(2) +
14+
"And if one green bottle should accidentally fall,\n" + lastLine(bottles - 1)
15+
16+
def recite(startBottles: Int, takeDown: Int): String =
17+
startBottles until startBottles - takeDown by -1 map verse mkString "\n"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"rabestro"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/main/scala/BottleSong.scala"
8+
],
9+
"test": [
10+
"src/test/scala/BottleSongTest.scala"
11+
],
12+
"example": [
13+
".meta/Example.scala"
14+
]
15+
},
16+
"blurb": "Produce the lyrics to the popular children's repetitive song: Ten Green Bottles.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Ten_Green_Bottles"
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
[d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03]
13+
description = "verse -> single verse -> first generic verse"
14+
15+
[0f0aded3-472a-4c64-b842-18d4f1f5f030]
16+
description = "verse -> single verse -> last generic verse"
17+
18+
[f61f3c97-131f-459e-b40a-7428f3ed99d9]
19+
description = "verse -> single verse -> verse with 2 bottles"
20+
21+
[05eadba9-5dbd-401e-a7e8-d17cc9baa8e0]
22+
description = "verse -> single verse -> verse with 1 bottle"
23+
24+
[a4a28170-83d6-4dc1-bd8b-319b6abb6a80]
25+
description = "lyrics -> multiple verses -> first two verses"
26+
27+
[3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db]
28+
description = "lyrics -> multiple verses -> last three verses"
29+
30+
[28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28]
31+
description = "lyrics -> multiple verses -> all verses"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
scalaVersion := "3.4.2"
2+
3+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % Test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.10.1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object BottleSong:
2+
3+
def recite(startBottles: Int, takeDown: Int): String = ???
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import org.scalatest.funsuite.AnyFunSuite
2+
import org.scalatest.matchers.should.Matchers
3+
4+
class BottleSongTest extends AnyFunSuite with Matchers {
5+
6+
test("first generic verse") {
7+
BottleSong.recite(10, 1) shouldBe
8+
"""Ten green bottles hanging on the wall,
9+
|Ten green bottles hanging on the wall,
10+
|And if one green bottle should accidentally fall,
11+
|There'll be nine green bottles hanging on the wall.
12+
|""".stripMargin.trim + "\n"
13+
}
14+
15+
test("last generic verse") {
16+
pending
17+
BottleSong.recite(3, 1) shouldBe
18+
"""Three green bottles hanging on the wall,
19+
|Three green bottles hanging on the wall,
20+
|And if one green bottle should accidentally fall,
21+
|There'll be two green bottles hanging on the wall.
22+
|""".stripMargin.trim + "\n"
23+
}
24+
25+
test("verse with two bottles") {
26+
pending
27+
BottleSong.recite(2, 1) shouldBe
28+
"""Two green bottles hanging on the wall,
29+
|Two green bottles hanging on the wall,
30+
|And if one green bottle should accidentally fall,
31+
|There'll be one green bottle hanging on the wall.
32+
|""".stripMargin.trim + "\n"
33+
}
34+
35+
test("verse with one bottle") {
36+
pending
37+
BottleSong.recite(1, 1) shouldBe
38+
"""One green bottle hanging on the wall,
39+
|One green bottle hanging on the wall,
40+
|And if one green bottle should accidentally fall,
41+
|There'll be no green bottles hanging on the wall.
42+
|""".stripMargin.trim + "\n"
43+
}
44+
45+
test("first two verses") {
46+
pending
47+
BottleSong.recite(10, 2) shouldBe
48+
"""Ten green bottles hanging on the wall,
49+
|Ten green bottles hanging on the wall,
50+
|And if one green bottle should accidentally fall,
51+
|There'll be nine green bottles hanging on the wall.
52+
|
53+
|Nine green bottles hanging on the wall,
54+
|Nine green bottles hanging on the wall,
55+
|And if one green bottle should accidentally fall,
56+
|There'll be eight green bottles hanging on the wall.
57+
|""".stripMargin.trim + "\n"
58+
}
59+
60+
test("last three verses") {
61+
pending
62+
BottleSong.recite(3, 3) shouldBe
63+
"""Three green bottles hanging on the wall,
64+
|Three green bottles hanging on the wall,
65+
|And if one green bottle should accidentally fall,
66+
|There'll be two green bottles hanging on the wall.
67+
|
68+
|Two green bottles hanging on the wall,
69+
|Two green bottles hanging on the wall,
70+
|And if one green bottle should accidentally fall,
71+
|There'll be one green bottle hanging on the wall.
72+
|
73+
|One green bottle hanging on the wall,
74+
|One green bottle hanging on the wall,
75+
|And if one green bottle should accidentally fall,
76+
|There'll be no green bottles hanging on the wall.
77+
|""".stripMargin.trim + "\n"
78+
}
79+
80+
test("all verses") {
81+
pending
82+
BottleSong.recite(10, 10) shouldBe
83+
"""Ten green bottles hanging on the wall,
84+
|Ten green bottles hanging on the wall,
85+
|And if one green bottle should accidentally fall,
86+
|There'll be nine green bottles hanging on the wall.
87+
|
88+
|Nine green bottles hanging on the wall,
89+
|Nine green bottles hanging on the wall,
90+
|And if one green bottle should accidentally fall,
91+
|There'll be eight green bottles hanging on the wall.
92+
|
93+
|Eight green bottles hanging on the wall,
94+
|Eight green bottles hanging on the wall,
95+
|And if one green bottle should accidentally fall,
96+
|There'll be seven green bottles hanging on the wall.
97+
|
98+
|Seven green bottles hanging on the wall,
99+
|Seven green bottles hanging on the wall,
100+
|And if one green bottle should accidentally fall,
101+
|There'll be six green bottles hanging on the wall.
102+
|
103+
|Six green bottles hanging on the wall,
104+
|Six green bottles hanging on the wall,
105+
|And if one green bottle should accidentally fall,
106+
|There'll be five green bottles hanging on the wall.
107+
|
108+
|Five green bottles hanging on the wall,
109+
|Five green bottles hanging on the wall,
110+
|And if one green bottle should accidentally fall,
111+
|There'll be four green bottles hanging on the wall.
112+
|
113+
|Four green bottles hanging on the wall,
114+
|Four green bottles hanging on the wall,
115+
|And if one green bottle should accidentally fall,
116+
|There'll be three green bottles hanging on the wall.
117+
|
118+
|Three green bottles hanging on the wall,
119+
|Three green bottles hanging on the wall,
120+
|And if one green bottle should accidentally fall,
121+
|There'll be two green bottles hanging on the wall.
122+
|
123+
|Two green bottles hanging on the wall,
124+
|Two green bottles hanging on the wall,
125+
|And if one green bottle should accidentally fall,
126+
|There'll be one green bottle hanging on the wall.
127+
|
128+
|One green bottle hanging on the wall,
129+
|One green bottle hanging on the wall,
130+
|And if one green bottle should accidentally fall,
131+
|There'll be no green bottles hanging on the wall.
132+
|""".stripMargin.trim + "\n"
133+
}
134+
}

0 commit comments

Comments
 (0)