Skip to content
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

Add bottle-song exercise #2507

Merged
merged 1 commit into from
Oct 3, 2023
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
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,18 @@
"difficulty": 6,
"status": "deprecated"
},
{
"slug": "bottle-song",
"name": "Bottle Song",
"uuid": "3fa6750f-cf01-4542-a494-df9a8c658733",
"practices": [
"strings"
],
"prerequisites": [
"strings"
],
"difficulty": 6
},
{
"slug": "food-chain",
"name": "Food Chain",
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.
```
19 changes: 19 additions & 0 deletions exercises/practice/bottle-song/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"kahgoh"
],
"files": {
"solution": [
"src/main/java/BottleSong.java"
],
"test": [
"src/test/java/BottleSongTest.java"
],
"example": [
".meta/src/reference/java/BottleSong.java"
]
},
"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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class BottleSong {
private String word(int number) {
return switch (number) {
case 2 -> "two";
case 3 -> "three";
case 4 -> "four";
case 5 -> "five";
case 6 -> "six";
case 7 -> "seven";
case 8 -> "eight";
case 9 -> "nine";
case 10 -> "ten";
default -> throw new IllegalArgumentException("Unrecognised number: " + Integer.toString(number));
};
}

private String verse(int number) {
return switch (number) {
case 1 ->
"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.\n";
case 2 ->
"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";
default -> {
var codePoints = word(number).codePoints().toArray();
codePoints[0] = Character.toUpperCase(codePoints[0]);
var numberText = new String(codePoints, 0, codePoints.length);

var nextNumberText = word(number - 1);

yield String.format(
"%s green bottles hanging on the wall,\n" +
"%s green bottles hanging on the wall,\n" +
"And if one green bottle should accidentally fall,\n" +
"There'll be %s green bottles hanging on the wall.\n",
numberText, numberText, nextNumberText);
}
};
}

String recite(int startBottles, int takeDown) {
var stop = startBottles - takeDown + 1;
var output = new StringBuilder();

for (var i = startBottles; i >= stop; i--) {
if (i != startBottles) {
output.append("\n");
}
output.append(verse(i));
}

return output.toString();
}
}
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"
24 changes: 24 additions & 0 deletions exercises/practice/bottle-song/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

// set default encoding to UTF-8
compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"

repositories {
mavenCentral()
}

dependencies {
testImplementation "junit:junit:4.13"
testImplementation "org.assertj:assertj-core:3.15.0"
}

test {
testLogging {
exceptionFormat = 'full'
showStandardStreams = true
events = ["passed", "failed", "skipped"]
}
}
7 changes: 7 additions & 0 deletions exercises/practice/bottle-song/src/main/java/BottleSong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class BottleSong {

String recite(int startBottles, int takeDown) {
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
}

}
152 changes: 152 additions & 0 deletions exercises/practice/bottle-song/src/test/java/BottleSongTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import org.junit.Test;
import org.junit.Ignore;
import org.junit.Before;

import static org.assertj.core.api.Assertions.assertThat;

public class BottleSongTest {

private BottleSong bottleSong;

@Before
public void setup() {
bottleSong = new BottleSong();
}

@Test
public void firstGenericVerse() {
assertThat(bottleSong.recite(10, 1)).isEqualTo(
"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"
);
}

@Ignore("Remove to run test")
@Test
public void lastGenericVerse() {
assertThat(bottleSong.recite(3, 1)).isEqualTo(
"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"
);
}

@Ignore("Remove to run test")
@Test
public void verseWithTwoBottles() {
assertThat(bottleSong.recite(2, 1)).isEqualTo(
"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"
);
}

@Ignore("Remove to run test")
@Test
public void verseWithOneBottle() {
assertThat(bottleSong.recite(1, 1)).isEqualTo(
"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.\n"
);
}

@Ignore("Remove to run test")
@Test
public void firstTwoVerses() {
assertThat(bottleSong.recite(10, 2)).isEqualTo(
"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"
);
}

@Ignore("Remove to run test")
@Test
public void lastThreeVerses() {
assertThat(bottleSong.recite(3, 3)).isEqualTo(
"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.\n"
);
}

@Ignore("Remove to run test")
@Test
public void allVerses() {
assertThat(bottleSong.recite(10, 10))
.isEqualTo(
"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.\n"
);
}
}
1 change: 1 addition & 0 deletions exercises/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ include 'practice:binary-search'
include 'practice:binary-search-tree'
include 'practice:bob'
include 'practice:book-store'
include 'practice:bottle-song'
include 'practice:bowling'
include 'practice:change'
include 'practice:circular-buffer'
Expand Down