Skip to content

beer-song: replace with bottle-song #2021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 20, 2025
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
17 changes: 12 additions & 5 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,23 @@
"slug": "beer-song",
"name": "Beer Song",
"uuid": "bb42bc3a-139d-4cab-8b3a-2eac2e1b77b6",
"practices": [
"loops",
"strings"
],
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": [
"case",
"vectors"
]
],
"status": "deprecated"
},
{
"slug": "bottle-song",
"name": "Bottle Song",
"uuid": "31e8185b-39d8-48c6-88a0-f302e2864f16",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
},
{
"slug": "difference-of-squares",
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.
```
2 changes: 2 additions & 0 deletions exercises/practice/bottle-song/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
20 changes: 20 additions & 0 deletions exercises/practice/bottle-song/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"authors": [
"ellnix"
],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/bottle-song.rs"
],
"example": [
".meta/example.rs"
]
},
"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"
}
39 changes: 39 additions & 0 deletions exercises/practice/bottle-song/.meta/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub fn recite(start_bottles: u32, take_down: u32) -> String {
(1..=take_down)
.map(|taken_down| {
let b = start_bottles - taken_down;
let b_word = number_to_eng_word(b).to_lowercase();
let b_noun = if b == 1 { "bottle" } else { "bottles" };
let prev_b = b + 1;
let prev_b_word = number_to_eng_word(prev_b);
let prev_b_noun = if prev_b == 1 { "bottle" } else { "bottles" };
[
format!("{prev_b_word} green {prev_b_noun} hanging on the wall"),
format!("{prev_b_word} green {prev_b_noun} hanging on the wall"),
"And if one green bottle should accidentally fall".into(),
format!("There'll be {b_word} green {b_noun} hanging on the wall."),
]
.join(",\n")
})
.collect::<Vec<String>>()
.join("\n\n")
+ "\n"
}

fn number_to_eng_word(digit: u32) -> String {
match digit {
0 => "No",
1 => "One",
2 => "Two",
3 => "Three",
4 => "Four",
5 => "Five",
6 => "Six",
7 => "Seven",
8 => "Eight",
9 => "Nine",
10 => "Ten",
_ => panic!("Didn't bother adding numbers past 10..."),
}
.to_string()
}
21 changes: 21 additions & 0 deletions exercises/practice/bottle-song/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use bottle_song::*;

{% for group in cases %}
{% for subgroup in group.cases %}
{% for test in subgroup.cases %}

#[test]
#[ignore]
fn {{ test.description | make_ident }}() {
assert_eq!(
recite({{ test.input.startBottles }}, {{ test.input.takeDown }}).trim(),
concat!(
{% for line in test.expected %}
"{{line}}{% if not loop.last %}\n{% endif %}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: keep the trailing newline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through other repos after receiving this feedback, I had only looked at Elixir which does not have the newline. Ruby seems to have the newline, and python, javascript, and go seem to return some sort of array of strings.

Should we just go for a Vec of Strings?

I'll keep the trailing new line in the meantime, even though I think that the challenge makes more sense without it. But it's no big deal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just go for a Vec of Strings?

Nah, that seems weird to me.

In my mind, text that consists of multiple lines should always have a trailing newline. But it's not important. Neither alternatives result in a more or less interesting / challenging exercise.

Maybe that's what the trim call was for. Allow students to implement their preference, with or without trailing newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add trim if you want, or else I guess we can merge this as is?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah whatever you want 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added trim(), feel free to merge if you have no reservations.

{% endfor %}
)
);
}
{% endfor -%}
{% endfor -%}
{% endfor -%}
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"
4 changes: 4 additions & 0 deletions exercises/practice/bottle-song/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
edition = "2021"
name = "bottle_song"
version = "1.0.0"
3 changes: 3 additions & 0 deletions exercises/practice/bottle-song/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn recite(start_bottles: u32, take_down: u32) -> String {
todo!("Return the bottle song starting at {start_bottles} and taking down {take_down} bottles")
}
158 changes: 158 additions & 0 deletions exercises/practice/bottle-song/tests/bottle-song.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
use bottle_song::*;

#[test]
fn first_generic_verse() {
assert_eq!(
recite(10, 1).trim(),
concat!(
"Ten green bottles hanging on the wall,\n",
"Ten green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be nine green bottles hanging on the wall.",
)
);
}

#[test]
#[ignore]
fn last_generic_verse() {
assert_eq!(
recite(3, 1).trim(),
concat!(
"Three green bottles hanging on the wall,\n",
"Three green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be two green bottles hanging on the wall.",
)
);
}

#[test]
#[ignore]
fn verse_with_2_bottles() {
assert_eq!(
recite(2, 1).trim(),
concat!(
"Two green bottles hanging on the wall,\n",
"Two green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be one green bottle hanging on the wall.",
)
);
}

#[test]
#[ignore]
fn verse_with_1_bottle() {
assert_eq!(
recite(1, 1).trim(),
concat!(
"One green bottle hanging on the wall,\n",
"One green bottle hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be no green bottles hanging on the wall.",
)
);
}

#[test]
#[ignore]
fn first_two_verses() {
assert_eq!(
recite(10, 2).trim(),
concat!(
"Ten green bottles hanging on the wall,\n",
"Ten green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be nine green bottles hanging on the wall.\n",
"\n",
"Nine green bottles hanging on the wall,\n",
"Nine green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be eight green bottles hanging on the wall.",
)
);
}

#[test]
#[ignore]
fn last_three_verses() {
assert_eq!(
recite(3, 3).trim(),
concat!(
"Three green bottles hanging on the wall,\n",
"Three green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be two green bottles hanging on the wall.\n",
"\n",
"Two green bottles hanging on the wall,\n",
"Two green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be one green bottle hanging on the wall.\n",
"\n",
"One green bottle hanging on the wall,\n",
"One green bottle hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be no green bottles hanging on the wall.",
)
);
}

#[test]
#[ignore]
fn all_verses() {
assert_eq!(
recite(10, 10).trim(),
concat!(
"Ten green bottles hanging on the wall,\n",
"Ten green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be nine green bottles hanging on the wall.\n",
"\n",
"Nine green bottles hanging on the wall,\n",
"Nine green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be eight green bottles hanging on the wall.\n",
"\n",
"Eight green bottles hanging on the wall,\n",
"Eight green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be seven green bottles hanging on the wall.\n",
"\n",
"Seven green bottles hanging on the wall,\n",
"Seven green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be six green bottles hanging on the wall.\n",
"\n",
"Six green bottles hanging on the wall,\n",
"Six green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be five green bottles hanging on the wall.\n",
"\n",
"Five green bottles hanging on the wall,\n",
"Five green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be four green bottles hanging on the wall.\n",
"\n",
"Four green bottles hanging on the wall,\n",
"Four green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be three green bottles hanging on the wall.\n",
"\n",
"Three green bottles hanging on the wall,\n",
"Three green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be two green bottles hanging on the wall.\n",
"\n",
"Two green bottles hanging on the wall,\n",
"Two green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be one green bottle hanging on the wall.\n",
"\n",
"One green bottle hanging on the wall,\n",
"One green bottle hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be no green bottles hanging on the wall.",
)
);
}