Skip to content

Commit 3cf03df

Browse files
Add bottle-song exercise (#242)
1 parent 899327b commit 3cf03df

File tree

7 files changed

+276
-0
lines changed

7 files changed

+276
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,14 @@
439439
"prerequisites": [],
440440
"difficulty": 4
441441
},
442+
{
443+
"slug": "bottle-song",
444+
"name": "Bottle Song",
445+
"uuid": "92fd7655-0e70-4f6d-9485-dbfbdce89dba",
446+
"practices": [],
447+
"prerequisites": [],
448+
"difficulty": 4
449+
},
442450
{
443451
"slug": "circular-buffer",
444452
"name": "Circular Buffer",
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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"bottle-song.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module main
2+
3+
import strings
4+
5+
const numbers = ['No', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine',
6+
'Ten']
7+
8+
fn append_line(mut builder strings.Builder, bottles int, lower bool) {
9+
if lower {
10+
builder.write_rune(numbers[bottles][0] | 32)
11+
builder.write_string(numbers[bottles][1..])
12+
} else {
13+
builder.write_string(numbers[bottles])
14+
}
15+
builder.write_string(' green bottle')
16+
if bottles != 1 {
17+
builder.write_rune(`s`)
18+
}
19+
builder.write_string(' hanging on the wall')
20+
}
21+
22+
fn recite(start_bottles int, take_down int) string {
23+
mut builder := strings.new_builder(4000)
24+
mut bottles := start_bottles
25+
for bottles > start_bottles - take_down {
26+
append_line(mut &builder, bottles, false)
27+
builder.write_string(',\n')
28+
29+
append_line(mut &builder, bottles, false)
30+
builder.write_string(',\n')
31+
32+
builder.write_string("And if one green bottle should accidentally fall,\nThere'll be ")
33+
bottles--
34+
35+
append_line(mut &builder, bottles, true)
36+
builder.write_string('.\n\n')
37+
}
38+
builder.go_back(2)
39+
return builder.str()
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03]
6+
description = "verse -> single verse -> first generic verse"
7+
8+
[0f0aded3-472a-4c64-b842-18d4f1f5f030]
9+
description = "verse -> single verse -> last generic verse"
10+
11+
[f61f3c97-131f-459e-b40a-7428f3ed99d9]
12+
description = "verse -> single verse -> verse with 2 bottles"
13+
14+
[05eadba9-5dbd-401e-a7e8-d17cc9baa8e0]
15+
description = "verse -> single verse -> verse with 1 bottle"
16+
17+
[a4a28170-83d6-4dc1-bd8b-319b6abb6a80]
18+
description = "lyrics -> multiple verses -> first two verses"
19+
20+
[3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db]
21+
description = "lyrics -> multiple verses -> last three verses"
22+
23+
[28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28]
24+
description = "lyrics -> multiple verses -> all verses"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module main
2+
3+
fn recite(start_bottles int, take_down int) string {
4+
}
+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
module main
2+
3+
fn test_verse__single_verse__first_generic_verse() {
4+
expected :=
5+
("Ten green bottles hanging on the wall,
6+
Ten green bottles hanging on the wall,
7+
And if one green bottle should accidentally fall,
8+
There'll be nine green bottles hanging on the wall.")
9+
assert recite(10, 1) == expected
10+
}
11+
12+
fn test_verse__single_verse__last_generic_verse() {
13+
expected :=
14+
("Three green bottles hanging on the wall,
15+
Three green bottles hanging on the wall,
16+
And if one green bottle should accidentally fall,
17+
There'll be two green bottles hanging on the wall.")
18+
assert recite(3, 1) == expected
19+
}
20+
21+
fn test_verse__single_verse__verse_with_2_bottles() {
22+
expected :=
23+
("Two green bottles hanging on the wall,
24+
Two green bottles hanging on the wall,
25+
And if one green bottle should accidentally fall,
26+
There'll be one green bottle hanging on the wall.")
27+
assert recite(2, 1) == expected
28+
}
29+
30+
fn test_verse__single_verse__verse_with_1_bottle() {
31+
expected :=
32+
("One green bottle hanging on the wall,
33+
One green bottle hanging on the wall,
34+
And if one green bottle should accidentally fall,
35+
There'll be no green bottles hanging on the wall.")
36+
assert recite(1, 1) == expected
37+
}
38+
39+
fn test_lyrics__multiple_verses__first_two_verses() {
40+
expected :=
41+
("Ten green bottles hanging on the wall,
42+
Ten green bottles hanging on the wall,
43+
And if one green bottle should accidentally fall,
44+
There'll be nine green bottles hanging on the wall.
45+
46+
Nine green bottles hanging on the wall,
47+
Nine green bottles hanging on the wall,
48+
And if one green bottle should accidentally fall,
49+
There'll be eight green bottles hanging on the wall.")
50+
assert recite(10, 2) == expected
51+
}
52+
53+
fn test_lyrics__multiple_verses__last_three_verses() {
54+
expected :=
55+
("Three green bottles hanging on the wall,
56+
Three green bottles hanging on the wall,
57+
And if one green bottle should accidentally fall,
58+
There'll be two green bottles hanging on the wall.
59+
60+
Two green bottles hanging on the wall,
61+
Two green bottles hanging on the wall,
62+
And if one green bottle should accidentally fall,
63+
There'll be one green bottle hanging on the wall.
64+
65+
One green bottle hanging on the wall,
66+
One green bottle hanging on the wall,
67+
And if one green bottle should accidentally fall,
68+
There'll be no green bottles hanging on the wall.")
69+
assert recite(3, 3) == expected
70+
}
71+
72+
fn test_lyrics__multiple_verses__all_verses() {
73+
expected :=
74+
("Ten green bottles hanging on the wall,
75+
Ten green bottles hanging on the wall,
76+
And if one green bottle should accidentally fall,
77+
There'll be nine green bottles hanging on the wall.
78+
79+
Nine green bottles hanging on the wall,
80+
Nine green bottles hanging on the wall,
81+
And if one green bottle should accidentally fall,
82+
There'll be eight green bottles hanging on the wall.
83+
84+
Eight green bottles hanging on the wall,
85+
Eight green bottles hanging on the wall,
86+
And if one green bottle should accidentally fall,
87+
There'll be seven green bottles hanging on the wall.
88+
89+
Seven green bottles hanging on the wall,
90+
Seven green bottles hanging on the wall,
91+
And if one green bottle should accidentally fall,
92+
There'll be six green bottles hanging on the wall.
93+
94+
Six green bottles hanging on the wall,
95+
Six green bottles hanging on the wall,
96+
And if one green bottle should accidentally fall,
97+
There'll be five green bottles hanging on the wall.
98+
99+
Five green bottles hanging on the wall,
100+
Five green bottles hanging on the wall,
101+
And if one green bottle should accidentally fall,
102+
There'll be four green bottles hanging on the wall.
103+
104+
Four green bottles hanging on the wall,
105+
Four green bottles hanging on the wall,
106+
And if one green bottle should accidentally fall,
107+
There'll be three green bottles hanging on the wall.
108+
109+
Three green bottles hanging on the wall,
110+
Three green bottles hanging on the wall,
111+
And if one green bottle should accidentally fall,
112+
There'll be two green bottles hanging on the wall.
113+
114+
Two green bottles hanging on the wall,
115+
Two green bottles hanging on the wall,
116+
And if one green bottle should accidentally fall,
117+
There'll be one green bottle hanging on the wall.
118+
119+
One green bottle hanging on the wall,
120+
One green bottle hanging on the wall,
121+
And if one green bottle should accidentally fall,
122+
There'll be no green bottles hanging on the wall.")
123+
assert recite(10, 10) == expected
124+
}

0 commit comments

Comments
 (0)