Skip to content

Commit 7a97b06

Browse files
Add bottle-song exercise (#73)
1 parent 8b195e7 commit 7a97b06

File tree

7 files changed

+178
-0
lines changed

7 files changed

+178
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,14 @@
353353
"prerequisites": [],
354354
"difficulty": 5
355355
},
356+
{
357+
"slug": "bottle-song",
358+
"name": "Bottle Song",
359+
"uuid": "d8725119-a411-4da7-98d0-26ea63e9e07c",
360+
"practices": [],
361+
"prerequisites": [],
362+
"difficulty": 5
363+
},
356364
{
357365
"slug": "crypto-square",
358366
"name": "Crypto Square",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"bottle_song.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
let numbers = "noonetwothreefourfivesixseveneightnineten"
2+
3+
let number_offsets = map1 i64.i32 [0, 2, 5, 8, 13, 17, 21, 24, 29, 34, 38, 41]
4+
5+
def green (bottles: i64): *[]u8 =
6+
numbers[number_offsets[bottles]:number_offsets[1 + bottles]] ++ " green bottle" ++ (if bottles != 1 then "s" else "")
7+
8+
def capitalize(phrase: *[]u8): *[]u8 =
9+
phrase with [0] = phrase[0] & 223
10+
11+
let verse (bottles: i64): []u8 =
12+
let first = (capitalize (green bottles)) ++ " hanging on the wall,\n"
13+
let second = first
14+
let third = "And if one green bottle should accidentally fall,\n"
15+
let fourth = "There'll be " ++ (green (bottles - 1)) ++ " hanging on the wall.\n\n"
16+
in
17+
first ++ second ++ third ++ fourth
18+
19+
def recite (start_bottles: i32) (take_down: i32): []u8 =
20+
let result = loop result = "" for fallen < take_down do
21+
result ++ verse (i64.i32 (start_bottles - fallen))
22+
in
23+
result[:length result - 1]
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def recite (start_bottles: i32) (take_down: i32): []u8 = ???
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import "bottle_song"
2+
3+
-- first generic verse
4+
-- ==
5+
-- input { 10 1 }
6+
-- output { "Ten green bottles hanging on the wall,\nTen green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be nine green bottles hanging on the wall.\n" }
7+
8+
-- last generic verse
9+
-- ==
10+
-- input { 3 1 }
11+
-- output { "Three green bottles hanging on the wall,\nThree green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be two green bottles hanging on the wall.\n" }
12+
13+
-- verse with 2 bottles
14+
-- ==
15+
-- input { 2 1 }
16+
-- output { "Two green bottles hanging on the wall,\nTwo green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be one green bottle hanging on the wall.\n" }
17+
18+
-- verse with 1 bottle
19+
-- ==
20+
-- input { 1 1 }
21+
-- output { "One green bottle hanging on the wall,\nOne green bottle hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be no green bottles hanging on the wall.\n" }
22+
23+
-- first two verses
24+
-- ==
25+
-- input { 10 2 }
26+
-- output { "Ten green bottles hanging on the wall,\nTen green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be nine green bottles hanging on the wall.\n\nNine green bottles hanging on the wall,\nNine green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be eight green bottles hanging on the wall.\n" }
27+
28+
-- last three verses
29+
-- ==
30+
-- input { 3 3 }
31+
-- output { "Three green bottles hanging on the wall,\nThree green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be two green bottles hanging on the wall.\n\nTwo green bottles hanging on the wall,\nTwo green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be one green bottle hanging on the wall.\n\nOne green bottle hanging on the wall,\nOne green bottle hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be no green bottles hanging on the wall.\n" }
32+
33+
-- all verses
34+
-- ==
35+
-- input { 10 10 }
36+
-- output { "Ten green bottles hanging on the wall,\nTen green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be nine green bottles hanging on the wall.\n\nNine green bottles hanging on the wall,\nNine green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be eight green bottles hanging on the wall.\n\nEight green bottles hanging on the wall,\nEight green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be seven green bottles hanging on the wall.\n\nSeven green bottles hanging on the wall,\nSeven green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be six green bottles hanging on the wall.\n\nSix green bottles hanging on the wall,\nSix green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be five green bottles hanging on the wall.\n\nFive green bottles hanging on the wall,\nFive green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be four green bottles hanging on the wall.\n\nFour green bottles hanging on the wall,\nFour green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be three green bottles hanging on the wall.\n\nThree green bottles hanging on the wall,\nThree green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be two green bottles hanging on the wall.\n\nTwo green bottles hanging on the wall,\nTwo green bottles hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be one green bottle hanging on the wall.\n\nOne green bottle hanging on the wall,\nOne green bottle hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be no green bottles hanging on the wall.\n" }
37+
38+
let main (start_bottles: i32) (take_down: i32): []u8 =
39+
recite start_bottles take_down

0 commit comments

Comments
 (0)