Skip to content

Commit 34b195d

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

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/onlinePlatform/codewars/5kyu/coding_with_squared_strings.cc

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ std::string joinVectorCharVector(const std::vector<std::vector<char> > &strings,
2222
}
2323

2424
std::vector<std::string> v;
25+
for (auto &chars: strings) {
26+
std::string char_string(chars.begin(), chars.end());
27+
v.push_back(char_string);
28+
}
2529

2630

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+
return joinStringVector(v, delimiter);
3133
}
3234

35+
3336
char FILLER_CHAR = (char) 11;
3437
std::string FILLER(1, FILLER_CHAR);
3538

@@ -38,9 +41,16 @@ class CodeSqStrings {
3841
static std::string code(const std::string &strng) {
3942
long n = std::ceil(std::sqrt(strng.length()));
4043

41-
std::vector temp(n, std::vector(n, FILLER_CHAR));
44+
std::vector temp(n, std::vector<char>(n, FILLER_CHAR));
45+
46+
for (int i = 0; i < strng.length(); ++i) {
47+
int y = i / n;
48+
int x = i % n;
49+
temp[y][x] = strng[i];
50+
}
51+
4252

43-
return joinStringVector(temp, "\n");
53+
return joinVectorCharVector(temp, "\n");
4454
}
4555

4656
static std::string decode(const std::string &strng) {

0 commit comments

Comments
 (0)