Skip to content

Commit c57cbb5

Browse files
committed
Implement recite function with constexpr arrays and proper newline formatting
1 parent 418be26 commit c57cbb5

5 files changed

Lines changed: 206 additions & 99 deletions

File tree

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,73 @@
1-
#include "twelve_days.h"
21
#include <array>
32
#include <string>
43

4+
#include "twelve_days.h"
5+
56
namespace twelve_days {
67

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+
}
2941
}
30-
}
31-
32-
result += ".";
33-
return result;
34-
}
3542

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;
4168
}
42-
result += verse(i);
69+
return result;
4370
}
44-
return result;
45-
}
46-
47-
std::string lyrics(int end_day) {
48-
return lyrics(1, end_day);
4971
}
5072

5173
} // namespace twelve_days

exercises/practice/twelve-days/.meta/example.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace twelve_days {
66

7-
std::string verse(int day);
8-
std::string lyrics(int start_day, int end_day);
9-
std::string lyrics(int end_day);
7+
std::string recite(int start_day, int end_day);
108

119
} // namespace twelve_days
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
#include "twelve_days.h"
2-
#include <vector>
3-
#include <string>
42

5-
namespace twelve_days {
6-
7-
} // namespace twelve_days
3+
namespace twelve_days {} // namespace twelve_days
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
#pragma once
22

3-
#include <string>
4-
5-
namespace twelve_days {
6-
7-
} // namespace twelve_days
3+
namespace twelve_days {} // namespace twelve_days

0 commit comments

Comments
 (0)