Fundamentals
Strings
Added overload for integers and templated overload for list/vector types. Had to add a special rule about whitespaces after a comma, since that is apparently a requirement in the test case, but that could be done probably a bit more nicely.
std::string digest(const std::string param) { std::string result; for (char letter: param) { const std::string whitespace = letter == ',' ? " " : " "; result += std::basic_string(1, letter) + whitespace; } result.pop_back(); return result; } std::string digest(const int param) { return digest(std::to_string(param)); } template<typename T> std::stringstream& operator<<(std::stringstream& out, std::initializer_list<T> v) { out << "{"; for (const auto& i : v) { out << i << ","; } std::string temp = out.str(); temp.pop_back(); out.str(temp); out.seekp(0, std::ios_base::end); out << "}"; std::cout << out.str(); return out; } template<typename T> std::string digest(std::initializer_list<T> param) { std::stringstream ss; ss << param; return digest(ss.str()); }
- std::string digest(const std::string param) {
- std::string result;
- for (char letter: param) {
result += std::basic_string(1, letter) + " ";- const std::string whitespace = letter == ',' ? " " : " ";
- result += std::basic_string(1, letter) + whitespace;
- }
- result.pop_back();
- return result;
}- }
- std::string digest(const int param) {
- return digest(std::to_string(param));
- }
- template<typename T>
- std::stringstream& operator<<(std::stringstream& out, std::initializer_list<T> v) {
- out << "{";
- for (const auto& i : v) {
- out << i << ",";
- }
- std::string temp = out.str();
- temp.pop_back();
- out.str(temp);
- out.seekp(0, std::ios_base::end);
- out << "}";
- std::cout << out.str();
- return out;
- }
- template<typename T>
- std::string digest(std::initializer_list<T> param) {
- std::stringstream ss;
- ss << param;
- return digest(ss.str());
- }
// TODO: Replace examples and use TDD by writing your own tests Describe(any_group_name_you_want) { It(should_do_something) { Assert::That(digest("Burger"), Equals("B u r g e r")); Assert::That(digest("FoEMfIp"), Equals("F o E M f I p")); Assert::That(digest("Haggis"), Equals("H a g g i s")); Assert::That(digest("chitlins"), Equals("c h i t l i n s")); Assert::That(digest("SPAM"), Equals("S P A M")); Assert::That(digest("S 1P 2A 3M"), Equals("S 1 P 2 A 3 M")); Assert::That(digest(1234567), Equals("1 2 3 4 5 6 7")); Assert::That(digest({1,2,3,4,5,6,7}), Equals("{ 1 , 2 , 3 , 4 , 5 , 6 , 7 }")); //Assert::That(digest({1:'a',2:'b',3:'c'}), Equals("{ 1 : ' a ' , 2 : ' b ' , 3 : ' c ' }")); } };
- // TODO: Replace examples and use TDD by writing your own tests
- Describe(any_group_name_you_want)
- {
- It(should_do_something)
- {
- Assert::That(digest("Burger"), Equals("B u r g e r"));
- Assert::That(digest("FoEMfIp"), Equals("F o E M f I p"));
- Assert::That(digest("Haggis"), Equals("H a g g i s"));
- Assert::That(digest("chitlins"), Equals("c h i t l i n s"));
- Assert::That(digest("SPAM"), Equals("S P A M"));
- Assert::That(digest("S 1P 2A 3M"), Equals("S 1 P 2 A 3 M"));
//Assert::That(digest(1234567), Equals("1 2 3 4 5 6 7"));//Assert::That(digest({1,2,3,4,5,6,7}), Equals("{ 1 , 2 , 3 , 4 , 5 , 6 , 7 }""));- Assert::That(digest(1234567), Equals("1 2 3 4 5 6 7"));
- Assert::That(digest({1,2,3,4,5,6,7}), Equals("{ 1 , 2 , 3 , 4 , 5 , 6 , 7 }"));
- //Assert::That(digest({1:'a',2:'b',3:'c'}), Equals("{ 1 : ' a ' , 2 : ' b ' , 3 : ' c ' }"));
- }
- };