Skip to content

Commit 093cfc3

Browse files
committed
feat(Codewars): WIP onlinePlatform/codewars/5kyu/coding_with_squared_strings.cc
1 parent 6fd61c2 commit 093cfc3

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/onlinePlatform/codewars/5kyu/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@ target_link_libraries(
1919
GTest::gtest_main
2020
)
2121
gtest_discover_tests(square_matrix_multiplication_google_test)
22+
23+
add_executable(
24+
coding_with_squared_strings
25+
coding_with_squared_strings_google_test.cc
26+
)
27+
target_link_libraries(
28+
coding_with_squared_strings
29+
GTest::gtest_main
30+
)
31+
gtest_discover_tests(coding_with_squared_strings)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// https://www.codewars.com/kata/56fcc393c5957c666900024d/train/cpp
2+
3+
#include <string>
4+
#include <vector>
5+
6+
#include <cmath>
7+
#include <numeric> // For std::accumulate
8+
9+
std::string joinStringVector(const std::vector<std::string> &strings, const std::string &delimiter) {
10+
if (strings.empty()) {
11+
return "";
12+
}
13+
return std::accumulate(std::next(strings.begin()), strings.end(), strings[0],
14+
[&](const std::string &a, const std::string &b) {
15+
return a + delimiter + b;
16+
});
17+
}
18+
19+
std::string joinVectorCharVector(const std::vector<std::vector<char> > &strings, const std::string &delimiter) {
20+
if (strings.empty()) {
21+
return "";
22+
}
23+
24+
std::vector<std::string> v;
25+
26+
27+
return std::accumulate(std::next(strings.begin()), strings.end(), strings[0],
28+
[&](const std::string &a, const std::string &b) {
29+
return a + delimiter + b;
30+
});
31+
}
32+
33+
char FILLER_CHAR = (char) 11;
34+
std::string FILLER(1, FILLER_CHAR);
35+
36+
class CodeSqStrings {
37+
public:
38+
static std::string code(const std::string &strng) {
39+
long n = std::ceil(std::sqrt(strng.length()));
40+
41+
std::vector temp(n, std::vector(n, FILLER_CHAR));
42+
43+
return joinStringVector(temp, "\n");
44+
}
45+
46+
static std::string decode(const std::string &strng) {
47+
return "";
48+
}
49+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <gtest/gtest.h>
2+
#include <string>
3+
#include <vector>
4+
5+
#include "coding_with_squared_strings.cc"
6+
7+
TEST(CodingWithSquaredStrings, Tests_CodeDecode) {
8+
std::string d = "What do you remember? When I looked at his streaky glasses, I wanted "
9+
"to leave him. And before that? He stole those cherries for me at midnight. We were walking "
10+
"in the rain and I loved him. And before that? I saw him coming "
11+
"toward me that time at the picnic, edgy, foreign.";
12+
std::string s = "\013ctg?.nadr d gdbW\n\013,i lnis tl eh\n\013 mtIAakietboaara\n\013eeo nnigsoe st?t\n\013d wsddnh lfls \n\013gaaa gtfeoeehWd\n"
13+
"\013ytrwbI .o rasiho\n\013, d e i rtev,se \n\013 t hflnW h e ny\n\013fhmioo emot Is o\n\013oeemrvt eshh tIu\n\013r eehw eaiwr \n"
14+
"\013eptc deea tmaelr\n\013iihot rtc?.naoe\n\013gcamhhre h tkom\n\013nntiaia meHAeyke\n\013.i ntmiwirend em";
15+
EXPECT_EQ(CodeSqStrings::code(d), s);
16+
17+
d = "Some say the world will end in fire, Some say in ice. From what I've tasted of desire "
18+
"I hold with those who favor fire. But if it had to perish twice, I think I know enough of hate"
19+
" To say that for destruction ice Is also great And would suffice.";
20+
s = "fa h ttrheI ilS\nitifakw s'irdo\nc cotnihftivce m\neAereocaihree,we\n.n wedroe . i \n\013dIdT , es t Sls\n\013 seoe t.eIaFola\n"
21+
"\013w s nIo srm y\n\013oatso Bwhtoee \n\013ulrautpuhoem nt\n\013lsuyghetold sdh\n\013doc hir d wa e\n\013 tt niif ohyi \n\013sgihoksfawfa nw\n"
22+
"\013uroaf h vi ti o\n\013fent I iotd nfr";
23+
EXPECT_EQ(CodeSqStrings::code(d), s);
24+
}

0 commit comments

Comments
 (0)