Skip to content

Commit c00fd6b

Browse files
marcelweikumIsaacG
authored andcommitted
Added bottle-song exercise
1 parent 8d139cb commit c00fd6b

12 files changed

Lines changed: 18324 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,22 @@
12951295
"practices": [],
12961296
"prerequisites": [],
12971297
"difficulty": 3
1298+
},
1299+
{
1300+
"slug": "bottle-song",
1301+
"name": "Bottle Song",
1302+
"uuid": "ed066c62-3a6a-480b-b72a-d1ae33369cd7",
1303+
"practices": [
1304+
"switch",
1305+
"loops",
1306+
"if-statements"
1307+
],
1308+
"prerequisites": [
1309+
"strings",
1310+
"numbers",
1311+
"loops"
1312+
],
1313+
"difficulty": 3
12981314
}
12991315
],
13001316
"foregone": [
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"authors": [
3+
"marcelweikum"
4+
],
5+
"files": {
6+
"solution": [
7+
"bottle_song.cpp",
8+
"bottle_song.h"
9+
],
10+
"test": [
11+
"bottle_song_test.cpp"
12+
],
13+
"example": [
14+
".meta/example.cpp",
15+
".meta/example.h"
16+
]
17+
},
18+
"blurb": "Produce the lyrics to the popular children's repetitive song: Ten Green Bottles.",
19+
"source": "Wikipedia",
20+
"source_url": "https://en.wikipedia.org/wiki/Ten_Green_Bottles"
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "bottle_song.h"
2+
3+
namespace bottle_song {
4+
5+
static const std::string wordsCap[] = {"Zero", "One", "Two", "Three",
6+
"Four", "Five", "Six", "Seven",
7+
"Eight", "Nine", "Ten"};
8+
static const std::string wordsLower[] = {"no", "one", "two", "three",
9+
"four", "five", "six", "seven",
10+
"eight", "nine", "ten"};
11+
12+
std::vector<std::string> recite(int startBottles, int takeDown) {
13+
std::vector<std::string> verses;
14+
int end = startBottles - takeDown + 1;
15+
for (int bottles = startBottles; bottles >= end; --bottles) {
16+
int next = bottles - 1;
17+
verses.push_back(wordsCap[bottles] + " green bottle" +
18+
(bottles == 1 ? "" : "s") + " hanging on the wall,");
19+
verses.push_back(wordsCap[bottles] + " green bottle" +
20+
(bottles == 1 ? "" : "s") + " hanging on the wall,");
21+
verses.push_back("And if one green bottle should accidentally fall,");
22+
std::string nextWord =
23+
(next >= 0 && next <= 10) ? wordsLower[next] : std::to_string(next);
24+
verses.push_back("There'll be " + nextWord + " green bottle" +
25+
(next == 1 ? "" : "s") + " hanging on the wall.");
26+
if (bottles > end) {
27+
verses.push_back("");
28+
}
29+
}
30+
return verses;
31+
}
32+
33+
} // namespace bottle_song
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
6+
namespace bottle_song {
7+
8+
std::vector<std::string> recite(int startBottles, int takeDown);
9+
10+
} // namespace bottle_song
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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Get the exercise name from the current directory
2+
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)
3+
4+
# Basic CMake project
5+
cmake_minimum_required(VERSION 3.5.1)
6+
7+
# Name the project after the exercise
8+
project(${exercise} CXX)
9+
10+
# Get a source filename from the exercise name by replacing -'s with _'s
11+
string(REPLACE "-" "_" file ${exercise})
12+
13+
# Implementation could be only a header
14+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
15+
set(exercise_cpp ${file}.cpp)
16+
else()
17+
set(exercise_cpp "")
18+
endif()
19+
20+
# Use the common Catch library?
21+
if(EXERCISM_COMMON_CATCH)
22+
# For Exercism track development only
23+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
24+
elseif(EXERCISM_TEST_SUITE)
25+
# The Exercism test suite is being run, the Docker image already
26+
# includes a pre-built version of Catch.
27+
find_package(Catch2 REQUIRED)
28+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
29+
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
30+
# When Catch is installed system wide we need to include a different
31+
# header, we need this define to use the correct one.
32+
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
33+
else()
34+
# Build executable from sources and headers
35+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
36+
endif()
37+
38+
set_target_properties(${exercise} PROPERTIES
39+
CXX_STANDARD 17
40+
CXX_STANDARD_REQUIRED OFF
41+
CXX_EXTENSIONS OFF
42+
)
43+
44+
set(CMAKE_BUILD_TYPE Debug)
45+
46+
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
47+
set_target_properties(${exercise} PROPERTIES
48+
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
49+
)
50+
endif()
51+
52+
# Configure to run all the tests?
53+
if(${EXERCISM_RUN_ALL_TESTS})
54+
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
55+
endif()
56+
57+
# Tell MSVC not to warn us about unchecked iterators in debug builds
58+
# Treat warnings as errors
59+
# Treat type conversion warnings C4244 and C4267 as level 4 warnings, i.e. ignore them in level 3
60+
if(${MSVC})
61+
set_target_properties(${exercise} PROPERTIES
62+
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS
63+
COMPILE_FLAGS "/WX /w44244 /w44267")
64+
endif()
65+
66+
# Run the tests on every build
67+
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "bottle_song.h"
2+
3+
namespace bottle_song {
4+
5+
// TODO: add your solution here
6+
7+
} // namespace bottle_song
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
namespace bottle_song {
4+
5+
// TODO: add your solution here
6+
7+
} // namespace bottle_song

0 commit comments

Comments
 (0)