-
-
Notifications
You must be signed in to change notification settings - Fork 237
Add twelve-days practice exercise #992
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
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
611f687
Add twelve-days practice exercise
alienx5499 ada7fd8
Remove stub function signatures from twelve-days exercise
alienx5499 67572b8
Fix twelve-days test: remove using namespace std, use std::string_vie…
alienx5499 af0742e
Update twelve-days test to match canonical data exactly: shorter desc…
alienx5499 cf7438e
Fix string concatenation in twelve-days example: split into separate …
alienx5499 e65a569
Fix include in twelve-days example: use twelve_days.h instead of exam…
alienx5499 c237f28
Optimize twelve-days example: use std::array instead of std::vector f…
alienx5499 4007cd4
Fix string concatenation in twelve-days example: use auto and std::st…
alienx5499 c58c572
Add missing EOL character to twelve-days CMakeLists.txt
alienx5499 2edf9c7
Use std::string{} constructor approach for string concatenation in tw…
alienx5499 418be26
Merge branch 'main' of https://github.com/exercism/cpp into add-twelv…
alienx5499 c57cbb5
Implement recite function with constexpr arrays and proper newline fo…
alienx5499 5184230
Add const qualifier to expected variables in twelve-days tests
alienx5499 d3b67ff
Update namespace structure in twelve-days exercise
alienx5499 3aeda3d
Reduce redundancy
ahans de9e3b2
fix formatting
ahans 24d8f9d
fix trailing whitespace
ahans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #include <array> | ||
| #include <string> | ||
|
|
||
| #include "twelve_days.h" | ||
|
|
||
| namespace twelve_days { | ||
|
|
||
| std::string recite(int start_day, int end_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"}; | ||
|
|
||
| if (start_day == end_day) { | ||
| // Single verse case | ||
| auto result = std::string{"On the "} + | ||
| std::string{ordinals[start_day]} + | ||
| " day of Christmas my true love gave to me: "; | ||
|
|
||
| for (int i = start_day; i >= 1; --i) { | ||
| if (i == start_day) { | ||
| result += std::string{gifts[i]}; | ||
| } else if (i == 1) { | ||
| result += std::string{", and "} + std::string{gifts[i]}; | ||
| } else { | ||
| result += std::string{", "} + std::string{gifts[i]}; | ||
| } | ||
| } | ||
|
|
||
| result += ".\n"; | ||
| return result; | ||
| } else { | ||
| // Multiple verses case | ||
| std::string result = ""; | ||
| for (int i = start_day; i <= end_day; ++i) { | ||
| if (i > start_day) { | ||
| result += "\n"; | ||
| } | ||
|
|
||
| auto verse = std::string{"On the "} + std::string{ordinals[i]} + | ||
| " day of Christmas my true love gave to me: "; | ||
|
|
||
| for (int j = i; j >= 1; --j) { | ||
| if (j == i) { | ||
| verse += std::string{gifts[j]}; | ||
| } else if (j == 1) { | ||
| verse += std::string{", and "} + std::string{gifts[j]}; | ||
| } else { | ||
| verse += std::string{", "} + std::string{gifts[j]}; | ||
| } | ||
| } | ||
|
|
||
| verse += ".\n"; | ||
| result += verse; | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| } // namespace twelve_days | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's quite a bit of redundancy in here. You had that better in a previous iteration. But don't worry, I will take care of it.