Skip to content

Commit 875e73b

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

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

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

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,71 @@ std::string joinVectorCharVector(const std::vector<std::vector<char> > &strings,
2727
v.push_back(char_string);
2828
}
2929

30+
return joinStringVector(v, delimiter);
31+
}
3032

33+
void rotateClockwise(std::vector<std::vector<char> > &v, int layer) {
34+
// TODO: assume v is square-shaped
35+
int n = v.size();
36+
int top = layer;
37+
int bottom = n - 1 - layer;
38+
int left = layer;
39+
int right = n - 1 - layer;
3140

32-
return joinStringVector(v, delimiter);
41+
if (left >= right) {
42+
return;
43+
}
44+
45+
int d = 0;
46+
while (left + d < right) {
47+
auto t = v[top][left + d];
48+
v[top][left + d] = v[bottom - d][left];
49+
v[bottom - d][left] = v[bottom][right - d];
50+
v[bottom][right - d] = v[top + d][right];
51+
v[top + d][right] = t;
52+
53+
d++;
54+
}
55+
rotateClockwise(v, layer + 1);
3356
}
3457

58+
void rotateCounterClockwise(std::vector<std::vector<char> > &v, int layer) {
59+
// TODO: assume v is square-shaped
60+
int n = v.size();
61+
int top = layer;
62+
int bottom = n - 1 - layer;
63+
int left = layer;
64+
int right = n - 1 - layer;
65+
66+
if (left >= right) {
67+
return;
68+
}
69+
70+
int d = 0;
71+
while (left + d < right) {
72+
auto t = v[top][left + d];
73+
v[top][left + d] =v[top + d][right];
74+
v[top + d][right] = v[bottom][right - d];
75+
v[bottom][right - d] = v[bottom - d][left];
76+
v[bottom - d][left] = t;
77+
78+
d++;
79+
}
80+
rotateClockwise(v, layer + 1);
81+
}
82+
83+
std::vector<std::vector<char>> toCharsVectors(const std::string &s) {
84+
int n = (int) std::sqrt(s.length());
85+
86+
std::vector<std::vector<char>> v;
87+
for (int i = 0; i < s.length(); i += n) {
88+
auto ss = s.substr(i, n);
89+
std::vector<char> chars(ss.begin(), ss.end());
90+
v.push_back(chars);
91+
}
92+
93+
return v;
94+
}
3595

3696
char FILLER_CHAR = (char) 11;
3797
std::string FILLER(1, FILLER_CHAR);
@@ -43,17 +103,32 @@ class CodeSqStrings {
43103

44104
std::vector temp(n, std::vector<char>(n, FILLER_CHAR));
45105

106+
// fill strng into temp
46107
for (int i = 0; i < strng.length(); ++i) {
47108
int y = i / n;
48109
int x = i % n;
49110
temp[y][x] = strng[i];
50111
}
51112

113+
// rotate 90 degrees clockwise
114+
rotateClockwise(temp, 0);
52115

53116
return joinVectorCharVector(temp, "\n");
54117
}
55118

56119
static std::string decode(const std::string &strng) {
57-
return "";
120+
auto temp = toCharsVectors(strng);
121+
122+
// rotate 90 degrees counter-clockwise
123+
rotateCounterClockwise(temp, 0);
124+
125+
auto s = joinVectorCharVector(temp, "");
126+
127+
std::string::size_type loc = s.find( FILLER, 0 );
128+
if( loc != std::string::npos ) {
129+
return s;
130+
} else {
131+
return s.substr(0, loc);
132+
}
58133
}
59134
};

0 commit comments

Comments
 (0)