Skip to content

Commit f683516

Browse files
authored
Implement Exercise Bottle Song (#148)
1 parent c2b3e04 commit f683516

File tree

7 files changed

+388
-0
lines changed

7 files changed

+388
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@
7878
"prerequisites": [],
7979
"difficulty": 2
8080
},
81+
{
82+
"slug": "bottle-song",
83+
"name": "Bottle Song",
84+
"uuid": "7de07a09-8554-4521-99a7-24a4c051196e",
85+
"practices": [],
86+
"prerequisites": [],
87+
"difficulty": 2
88+
},
8189
{
8290
"slug": "clock",
8391
"name": "Clock",
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"rmonnet"
4+
],
5+
"files": {
6+
"solution": [
7+
"bottle_song.odin"
8+
],
9+
"test": [
10+
"bottle_song_test.odin"
11+
],
12+
"example": [
13+
".meta/example.odin"
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package bottle_song
2+
3+
import "core:fmt"
4+
5+
recite :: proc(start_bottles, take_down: int) -> []string {
6+
7+
num_verses := take_down * 4 + (take_down - 1)
8+
song := make([dynamic]string, 0, num_verses)
9+
for i := 0; i < take_down; i += 1 {
10+
n := start_bottles - i
11+
// Need an empty line to separate paragraphs.
12+
if i > 0 {
13+
// This seems redundant, but if we don't have all the strings in the `[]string`
14+
// dynamically allocated, the `delete()` in the caller is having a hard time
15+
// figuring out which ones to free and which ones not to, resulting in a `bad free` warning.
16+
append(&song, fmt.aprintf(""))
17+
}
18+
verse1 := fmt.aprintf("%s green bottle%s hanging on the wall,", cap_nums[n], plural(n))
19+
append(&song, verse1)
20+
verse2 := fmt.aprintf("%s green bottle%s hanging on the wall,", cap_nums[n], plural(n))
21+
append(&song, verse2)
22+
23+
// Same comment as for "" being dynamic above.
24+
verse3 := fmt.aprintf("And if one green bottle should accidentally fall,")
25+
append(&song, verse3)
26+
27+
verse4 := fmt.aprintf(
28+
"There'll be %s green bottle%s hanging on the wall.",
29+
nums[n - 1],
30+
plural(n - 1),
31+
)
32+
append(&song, verse4)}
33+
return song[:]
34+
}
35+
36+
plural :: proc(n: int) -> string {
37+
38+
// We only use "bottle" (singular) for one (not "no bottles").
39+
if n == 1 { return "" }
40+
return "s"
41+
}
42+
43+
nums := [?]string {
44+
"no",
45+
"one",
46+
"two",
47+
"three",
48+
"four",
49+
"five",
50+
"six",
51+
"seven",
52+
"eight",
53+
"nine",
54+
"ten",
55+
}
56+
57+
cap_nums := [?]string {
58+
"No",
59+
"One",
60+
"Two",
61+
"Three",
62+
"Four",
63+
"Five",
64+
"Six",
65+
"Seven",
66+
"Eight",
67+
"Nine",
68+
"Ten",
69+
}
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package bottle_song
2+
3+
recite :: proc(start_bottles, take_down: int) -> []string {
4+
// Implement the procedure.
5+
return nil
6+
}

0 commit comments

Comments
 (0)