Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
611f687
Add twelve-days practice exercise
alienx5499 Jul 7, 2025
ada7fd8
Remove stub function signatures from twelve-days exercise
alienx5499 Aug 29, 2025
67572b8
Fix twelve-days test: remove using namespace std, use std::string_vie…
alienx5499 Aug 29, 2025
af0742e
Update twelve-days test to match canonical data exactly: shorter desc…
alienx5499 Aug 29, 2025
cf7438e
Fix string concatenation in twelve-days example: split into separate …
alienx5499 Aug 29, 2025
e65a569
Fix include in twelve-days example: use twelve_days.h instead of exam…
alienx5499 Aug 29, 2025
c237f28
Optimize twelve-days example: use std::array instead of std::vector f…
alienx5499 Aug 29, 2025
4007cd4
Fix string concatenation in twelve-days example: use auto and std::st…
alienx5499 Aug 29, 2025
c58c572
Add missing EOL character to twelve-days CMakeLists.txt
alienx5499 Aug 29, 2025
2edf9c7
Use std::string{} constructor approach for string concatenation in tw…
alienx5499 Aug 29, 2025
418be26
Merge branch 'main' of https://github.com/exercism/cpp into add-twelv…
alienx5499 Aug 30, 2025
c57cbb5
Implement recite function with constexpr arrays and proper newline fo…
alienx5499 Aug 30, 2025
5184230
Add const qualifier to expected variables in twelve-days tests
alienx5499 Aug 30, 2025
d3b67ff
Update namespace structure in twelve-days exercise
alienx5499 Aug 30, 2025
3aeda3d
Reduce redundancy
ahans Aug 30, 2025
de9e3b2
fix formatting
ahans Aug 30, 2025
24d8f9d
fix trailing whitespace
ahans Aug 30, 2025
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,14 @@
"comparisons"
],
"difficulty": 6
},
{
"slug": "twelve-days",
"name": "Twelve Days",
"uuid": "0daec3e8-a0d1-49e8-9de5-0ad73f9c3243",
"practices": [],
"prerequisites": [],
"difficulty": 3
}
],
"foregone": [
Expand Down
36 changes: 36 additions & 0 deletions exercises/practice/twelve-days/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Instructions

Your task in this exercise is to write code that returns the lyrics of the song: "The Twelve Days of Christmas."

"The Twelve Days of Christmas" is a common English Christmas carol.
Each subsequent verse of the song builds on the previous verse.

The lyrics your code returns should _exactly_ match the full song text shown below.

## Lyrics

```text
On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.

On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.

On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the twelfth day of Christmas my true love gave to me: twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
```
21 changes: 21 additions & 0 deletions exercises/practice/twelve-days/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"alienx5499"
],
"files": {
"solution": [
"twelve_days.cpp",
"twelve_days.h"
],
"test": [
"twelve_days_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"blurb": "Output the lyrics to 'The Twelve Days of Christmas'.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_(song)"
}
55 changes: 55 additions & 0 deletions exercises/practice/twelve-days/.meta/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <array>
#include <string>

#include "twelve_days.h"

namespace twelve_days {

namespace {
std::string verse(int day) {
constexpr std::array ordinals = {
"", "first", "second", "third", "fourth", "fifth", "sixth",
"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"};

constexpr std::array gifts = {"",
"a Partridge in a Pear Tree",
"two Turtle Doves",
"three French Hens",
"four Calling Birds",
"five Gold Rings",
"six Geese-a-Laying",
"seven Swans-a-Swimming",
"eight Maids-a-Milking",
"nine Ladies Dancing",
"ten Lords-a-Leaping",
"eleven Pipers Piping",
"twelve Drummers Drumming"};

auto result = std::string{"On the "} + ordinals[day] +
" day of Christmas my true love gave to me: " + gifts[day];

for (int i = day - 1; i >= 1; --i) {
if (i == 1) {
result += std::string{", and "};
} else {
result += std::string{", "};
}
result += gifts[i];
}

return result + ".\n";
}
} // namespace

std::string recite(int start_day, int end_day) {
std::string result{};
for (int i = start_day; i <= end_day; ++i) {
if (i > start_day) {
result += '\n';
}
result += verse(i);
}
return result;
}

} // namespace twelve_days
9 changes: 9 additions & 0 deletions exercises/practice/twelve-days/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <string>

namespace twelve_days {

std::string recite(int start_day, int end_day);

} // namespace twelve_days
55 changes: 55 additions & 0 deletions exercises/practice/twelve-days/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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.

[c0b5a5e6-c89d-49b1-a6b2-9f523bff33f7]
description = "verse -> first day a partridge in a pear tree"

[1c64508a-df3d-420a-b8e1-fe408847854a]
description = "verse -> second day two turtle doves"

[a919e09c-75b2-4e64-bb23-de4a692060a8]
description = "verse -> third day three french hens"

[9bed8631-ec60-4894-a3bb-4f0ec9fbe68d]
description = "verse -> fourth day four calling birds"

[cf1024f0-73b6-4545-be57-e9cea565289a]
description = "verse -> fifth day five gold rings"

[50bd3393-868a-4f24-a618-68df3d02ff04]
description = "verse -> sixth day six geese-a-laying"

[8f29638c-9bf1-4680-94be-e8b84e4ade83]
description = "verse -> seventh day seven swans-a-swimming"

[7038d6e1-e377-47ad-8c37-10670a05bc05]
description = "verse -> eighth day eight maids-a-milking"

[37a800a6-7a56-4352-8d72-0f51eb37cfe8]
description = "verse -> ninth day nine ladies dancing"

[10b158aa-49ff-4b2d-afc3-13af9133510d]
description = "verse -> tenth day ten lords-a-leaping"

[08d7d453-f2ba-478d-8df0-d39ea6a4f457]
description = "verse -> eleventh day eleven pipers piping"

[0620fea7-1704-4e48-b557-c05bf43967f0]
description = "verse -> twelfth day twelve drummers drumming"

[da8b9013-b1e8-49df-b6ef-ddec0219e398]
description = "lyrics -> recites first three verses of the song"

[c095af0d-3137-4653-ad32-bfb899eda24c]
description = "lyrics -> recites three verses from the middle of the song"

[20921bc9-cc52-4627-80b3-198cbbfcf9b7]
description = "lyrics -> recites the whole song"
67 changes: 67 additions & 0 deletions exercises/practice/twelve-days/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already
# includes a pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different
# header, we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
# Treat warnings as errors
# Treat type conversion warnings C4244 and C4267 as level 4 warnings, i.e. ignore them in level 3
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS
COMPILE_FLAGS "/WX /w44244 /w44267")
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
Loading
Loading