|
1 | | -#include "twelve_days.h" |
2 | 1 | #include <array> |
3 | 2 | #include <string> |
4 | 3 |
|
| 4 | +#include "twelve_days.h" |
| 5 | + |
5 | 6 | namespace twelve_days { |
6 | 7 |
|
7 | | -std::string verse(int day) { |
8 | | - const std::array<std::string, 13> ordinals = { |
9 | | - "", "first", "second", "third", "fourth", "fifth", "sixth", |
10 | | - "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" |
11 | | - }; |
12 | | - |
13 | | - const std::array<std::string, 13> gifts = { |
14 | | - "", "a Partridge in a Pear Tree", "two Turtle Doves", "three French Hens", |
15 | | - "four Calling Birds", "five Gold Rings", "six Geese-a-Laying", |
16 | | - "seven Swans-a-Swimming", "eight Maids-a-Milking", "nine Ladies Dancing", |
17 | | - "ten Lords-a-Leaping", "eleven Pipers Piping", "twelve Drummers Drumming" |
18 | | - }; |
19 | | - |
20 | | - auto result = std::string{"On the "} + ordinals[day] + " day of Christmas my true love gave to me: "; |
21 | | - |
22 | | - for (int i = day; i >= 1; --i) { |
23 | | - if (i == day) { |
24 | | - result += gifts[i]; |
25 | | - } else if (i == 1) { |
26 | | - result += std::string{", and "} + gifts[i]; |
27 | | - } else { |
28 | | - result += std::string{", "} + gifts[i]; |
| 8 | +std::string recite(int start_day, int end_day) { |
| 9 | + constexpr std::array<const char*, 13> ordinals = { |
| 10 | + "", "first", "second", "third", "fourth", "fifth", "sixth", |
| 11 | + "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"}; |
| 12 | + |
| 13 | + constexpr std::array<const char*, 13> gifts = {"", |
| 14 | + "a Partridge in a Pear Tree", |
| 15 | + "two Turtle Doves", |
| 16 | + "three French Hens", |
| 17 | + "four Calling Birds", |
| 18 | + "five Gold Rings", |
| 19 | + "six Geese-a-Laying", |
| 20 | + "seven Swans-a-Swimming", |
| 21 | + "eight Maids-a-Milking", |
| 22 | + "nine Ladies Dancing", |
| 23 | + "ten Lords-a-Leaping", |
| 24 | + "eleven Pipers Piping", |
| 25 | + "twelve Drummers Drumming"}; |
| 26 | + |
| 27 | + if (start_day == end_day) { |
| 28 | + // Single verse case |
| 29 | + auto result = std::string{"On the "} + |
| 30 | + std::string{ordinals[start_day]} + |
| 31 | + " day of Christmas my true love gave to me: "; |
| 32 | + |
| 33 | + for (int i = start_day; i >= 1; --i) { |
| 34 | + if (i == start_day) { |
| 35 | + result += std::string{gifts[i]}; |
| 36 | + } else if (i == 1) { |
| 37 | + result += std::string{", and "} + std::string{gifts[i]}; |
| 38 | + } else { |
| 39 | + result += std::string{", "} + std::string{gifts[i]}; |
| 40 | + } |
29 | 41 | } |
30 | | - } |
31 | | - |
32 | | - result += "."; |
33 | | - return result; |
34 | | -} |
35 | 42 |
|
36 | | -std::string lyrics(int start_day, int end_day) { |
37 | | - std::string result = ""; |
38 | | - for (int i = start_day; i <= end_day; ++i) { |
39 | | - if (i > start_day) { |
40 | | - result += "\n\n"; |
| 43 | + result += ".\n"; |
| 44 | + return result; |
| 45 | + } else { |
| 46 | + // Multiple verses case |
| 47 | + std::string result = ""; |
| 48 | + for (int i = start_day; i <= end_day; ++i) { |
| 49 | + if (i > start_day) { |
| 50 | + result += "\n"; |
| 51 | + } |
| 52 | + |
| 53 | + auto verse = std::string{"On the "} + std::string{ordinals[i]} + |
| 54 | + " day of Christmas my true love gave to me: "; |
| 55 | + |
| 56 | + for (int j = i; j >= 1; --j) { |
| 57 | + if (j == i) { |
| 58 | + verse += std::string{gifts[j]}; |
| 59 | + } else if (j == 1) { |
| 60 | + verse += std::string{", and "} + std::string{gifts[j]}; |
| 61 | + } else { |
| 62 | + verse += std::string{", "} + std::string{gifts[j]}; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + verse += ".\n"; |
| 67 | + result += verse; |
41 | 68 | } |
42 | | - result += verse(i); |
| 69 | + return result; |
43 | 70 | } |
44 | | - return result; |
45 | | -} |
46 | | - |
47 | | -std::string lyrics(int end_day) { |
48 | | - return lyrics(1, end_day); |
49 | 71 | } |
50 | 72 |
|
51 | 73 | } // namespace twelve_days |
0 commit comments