Fundamentals
Strings
The goal of this kumite is to separate each adjacient pair of characters in the original string by an additional space (' '
) character.
Original
Used std::accumulate
to achieve this
Fork 1
- if the string is empty, return empty string (next steps require the string to be nonempty)
- preallocate a buffer of size
2*string.length()-1
filled with space characters - map each each character of original string from position
i
to position2*i
in the buffer
#include <string> std::string digest(const std::string& str) { if(str.empty()) return ""; std::string buff (str.size()*2-1, ' '); for(std::size_t i = 0; i < str.size(); ++i) buff[2*i] = str[i]; return buff; }
- #include <string>
#include <numeric>std::string digest(const std::string& param) {return param.empty()?param: std::accumulate(std::next(param.cbegin()),param.cend(),std::string(1,param[0]),[](auto &s, auto c) {return s.append(" ").append(1,c);});- std::string digest(const std::string& str) {
- if(str.empty())
- return "";
- std::string buff (str.size()*2-1, ' ');
- for(std::size_t i = 0; i < str.size(); ++i)
- buff[2*i] = str[i];
- return buff;
- }
// 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 ' }")); // Check for the two special cases !! Assert::That(digest("B"), Equals("B")); Assert::That(digest(""), Equals("")); } };
- // 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 ' }"));- 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 ' }"));
- // Check for the two special cases !!
- Assert::That(digest("B"), Equals("B"));
- Assert::That(digest(""), Equals(""));
- }
- };