Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "line-up",
"name": "Line Up",
"uuid": "f7ab5511-8436-43b3-a804-6dc2d625e3e4",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "raindrops",
"name": "Raindrops",
Expand Down Expand Up @@ -97,6 +105,14 @@
"difficulty": 2,
"status": "deprecated"
},
{
"slug": "bottle-song",
"name": "Bottle Song",
"uuid": "ec751487-6801-4a8e-a4e3-513419aef901",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "collatz-conjecture",
"name": "Collatz Conjecture",
Expand Down Expand Up @@ -203,6 +219,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "resistor-color-trio",
"name": "Resistor Color Trio",
"uuid": "e7e4af93-ace3-4ff1-8309-6d6bbaa41368",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "rna-transcription",
"name": "RNA Transcription",
Expand Down Expand Up @@ -283,6 +307,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "twelve-days",
"name": "Twelve Days",
"uuid": "4f14f4f5-ea9f-4604-b773-a1e0e750357e",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "allergies",
"name": "Allergies",
Expand Down Expand Up @@ -483,6 +515,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "save-the-cow",
"name": "Save the Cow",
"uuid": "f1aa17d2-dca6-4b1b-8e02-4c62dc322f59",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "say",
"name": "Say",
Expand Down Expand Up @@ -603,6 +643,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "grade-school",
"name": "Grade School",
"uuid": "17c90f99-9cb8-4856-b89e-3732ef5d7575",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "house",
"name": "House",
Expand Down Expand Up @@ -651,6 +699,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "simple-cipher",
"name": "Simple Cipher",
"uuid": "0396ed11-d589-4165-8ce7-62ecc2a66721",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "variable-length-quantity",
"name": "Variable Length Quantity",
Expand All @@ -675,6 +731,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "dnd-character",
"name": "D&D Character",
"uuid": "972316c8-0f5d-4d32-b461-4240ba40924c",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "error-handling",
"name": "Error Handling",
Expand Down
57 changes: 57 additions & 0 deletions exercises/practice/bottle-song/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Instructions

Recite the lyrics to that popular children's repetitive song: Ten Green Bottles.

Note that not all verses are identical.

```text
Ten green bottles hanging on the wall,
Ten green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be nine green bottles hanging on the wall.

Nine green bottles hanging on the wall,
Nine green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be eight green bottles hanging on the wall.

Eight green bottles hanging on the wall,
Eight green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be seven green bottles hanging on the wall.

Seven green bottles hanging on the wall,
Seven green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be six green bottles hanging on the wall.

Six green bottles hanging on the wall,
Six green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be five green bottles hanging on the wall.

Five green bottles hanging on the wall,
Five green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be four green bottles hanging on the wall.

Four green bottles hanging on the wall,
Four green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be three green bottles hanging on the wall.

Three green bottles hanging on the wall,
Three green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be two green bottles hanging on the wall.

Two green bottles hanging on the wall,
Two green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be one green bottle hanging on the wall.

One green bottle hanging on the wall,
One green bottle hanging on the wall,
And if one green bottle should accidentally fall,
There'll be no green bottles hanging on the wall.
```
72 changes: 72 additions & 0 deletions exercises/practice/bottle-song/.meta/Example.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
BottleSong :: {}.{
recite : U8, U8 -> Try(Str, [InvalidVerseNumber, ..])
recite = |starting_number, number_of_verses| {
if starting_number == 0 or number_of_verses == 0 or starting_number < number_of_verses {
Err(InvalidVerseNumber)
} else {
((starting_number - number_of_verses + 1)..=starting_number)
.iter()
|> List.from_iter
.rev()
.map_try(verse)?
|> Str.join_with("\n\n")
|> Ok
}
}
}

verse : U8 -> Try(Str, [InvalidVerseNumber, ..])
verse = |number| {
\\${describe_bottles(number, Uppercase)?} hanging on the wall,
\\${describe_bottles(number, Uppercase)?} hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be ${describe_bottles(number - 1, Lowercase)?} hanging on the wall.
|> Ok
}

describe_bottles : U8, [Uppercase, Lowercase] -> Try(Str, [InvalidVerseNumber, ..])
describe_bottles = |number, case| {
if number > 10 {
return Err(InvalidVerseNumber)
}
number_str_upper : Str
number_str_upper = match number {
0 => "No"
1 => "One"
2 => "Two"
3 => "Three"
4 => "Four"
5 => "Five"
6 => "Six"
7 => "Seven"
8 => "Eight"
9 => "Nine"
10 => "Ten"
_ => crash "Unreachable"
}

number_str : Str
number_str = match case {
Uppercase => number_str_upper
Lowercase => number_str_upper |> lowercase_first_letter
}

maybe_s : Str
maybe_s = if number != 1 {
"s"
} else {
""
}

Ok("${number_str} green bottle${maybe_s}")
}

lowercase_first_letter : Str -> Str
lowercase_first_letter = |str| {
match str.to_utf8() {
[first, .. as rest] if first >= 'A' and first <= 'Z' => Str.from_utf8(rest.prepend(first - 'A' + 'a')) ?? {
crash "Unreachable"
}
_ => str
}
}
17 changes: 17 additions & 0 deletions exercises/practice/bottle-song/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [],
"files": {
"solution": [
"BottleSong.roc"
],
"test": [
"bottle-song-test.roc"
],
"example": [
".meta/Example.roc"
]
},
"blurb": "Produce the lyrics to the popular children's repetitive song: Ten Green Bottles.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Ten_Green_Bottles"
}
30 changes: 30 additions & 0 deletions exercises/practice/bottle-song/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{%- import "generator_macros.j2" as macros with context -%}
{{ macros.canonical_ref() }}
{{ macros.header() }}

import {{ exercise | to_pascal }} exposing [{{ properties | map("to_snake") | join(", ") }}]

{% macro render_cases(cases) %}
{%- for case in cases %}
{%- if "cases" in case %}
{{ render_cases(case["cases"]) }}
{%- else %}
# {{ case["description"] }}
expect {
result = {{ case["property"] | to_snake }}(
{%- for key, value in case["input"].items() -%}
{{ value | to_roc }}{% if not loop.last %}, {% endif %}
{%- endfor -%}
)?
{%- if case["expected"] is mapping and "error" in case["expected"] %}
result.is_err()
{%- else %}
result == {{ case["expected"] | join('\n') | to_roc_multiline_string | indent(8) }}
{%- endif %}
}
{%- endif %}
{%- endfor %}
{% endmacro %}

{{ render_cases(cases) }}
{{ macros.footer() }}
31 changes: 31 additions & 0 deletions exercises/practice/bottle-song/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03]
description = "verse -> single verse -> first generic verse"

[0f0aded3-472a-4c64-b842-18d4f1f5f030]
description = "verse -> single verse -> last generic verse"

[f61f3c97-131f-459e-b40a-7428f3ed99d9]
description = "verse -> single verse -> verse with 2 bottles"

[05eadba9-5dbd-401e-a7e8-d17cc9baa8e0]
description = "verse -> single verse -> verse with 1 bottle"

[a4a28170-83d6-4dc1-bd8b-319b6abb6a80]
description = "lyrics -> multiple verses -> first two verses"

[3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db]
description = "lyrics -> multiple verses -> last three verses"

[28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28]
description = "lyrics -> multiple verses -> all verses"
6 changes: 6 additions & 0 deletions exercises/practice/bottle-song/BottleSong.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BottleSong :: {}.{
recite : U8, U8 -> Try(Str, _)
recite = |starting_number, number_of_verses| {
crash "Please implement the 'recite' function"
}
}
Loading