-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump_cpp.cpp
More file actions
336 lines (295 loc) · 8.72 KB
/
Copy pathdump_cpp.cpp
File metadata and controls
336 lines (295 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "stdcpp.h"
#include <type_traits>
using namespace std;
using _JSON_ = nlohmann::json;
template<typename T> T& lv(T&& x) { return x; }
struct _TEST_ {
_JSON_ val;
};
inline void from_json(const _JSON_& j, _TEST_& t) {
j.at("val").get_to(t.val);
}
inline char jsonToChar(const _JSON_& j) { return j.get<string>()[0]; }
inline int jsonToInt(const _JSON_& j) { return j.get<int>(); }
inline long long jsonToLong(const _JSON_& j) { return j.get<long long>(); }
inline double jsonToDouble(const _JSON_& j) { return j.get<double>(); }
inline float jsonToFloat(const _JSON_& j) { return j.get<float>(); }
inline bool jsonToBool(const _JSON_& j) { return j.get<bool>(); }
inline string jsonToString(const _JSON_& j) {
return j.is_string() ? j.get<string>() : j.dump();
}
inline vector<int> jsonToIntArray(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<int> >();
}
inline vector<long long> jsonToLongArray(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<long long> >();
}
inline vector<double> jsonToDoubleArray(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<double> >();
}
inline vector<float> jsonToFloatArray(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<float> >();
}
inline vector<bool> jsonToBoolArray(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<bool> >();
}
inline vector<string> jsonToStringArray(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<string> >();
}
inline vector<optional<int>> jsonToOptionalIntArray(const _JSON_& j) {
if (j.is_null()) return {};
vector<optional<int>> out;
out.reserve(j.size());
for (const auto& x : j) {
if (x.is_null()) {
out.push_back(nullopt);
} else {
out.push_back(x.get<int>());
}
}
return out;
}
inline vector<char> jsonToCharArray(const _JSON_& j) {
if (j.is_null()) return {};
vector<char> out;
out.reserve(j.size());
for (const auto& x : j) out.push_back(jsonToChar(x));
return out;
}
inline vector<vector<int>> jsonToIntMatrix(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<vector<int> > >();
}
inline vector<vector<long long>> jsonToLongMatrix(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<vector<long long> > >();
}
inline vector<vector<double>> jsonToDoubleMatrix(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<vector<double> > >();
}
inline vector<vector<float>> jsonToFloatMatrix(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<vector<float> > >();
}
inline vector<vector<bool>> jsonToBoolMatrix(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<vector<bool> > >();
}
inline vector<vector<string>> jsonToStringMatrix(const _JSON_& j) {
if (j.is_null()) return {};
return j.get<vector<vector<string> > >();
}
inline vector<vector<char>> jsonToCharMatrix(const _JSON_& j) {
if (j.is_null()) return {};
vector<vector<char>> out;
out.reserve(j.size());
for (const auto& row : j) out.push_back(jsonToCharArray(row));
return out;
}
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
inline ListNode *toListNode(const vector<int> &arr) {
if (arr.empty())
return nullptr;
ListNode *head = new ListNode(arr[0]);
ListNode *cur = head;
for (int i = 1; i < (int)arr.size(); i++) {
cur->next = new ListNode(arr[i]);
cur = cur->next;
}
return head;
}
inline vector<int> listNodeToArray(ListNode *head) {
vector<int> res;
for (ListNode *cur = head; cur != nullptr; cur = cur->next)
res.push_back(cur->val);
return res;
}
inline vector<ListNode *> toLinkedLists(const vector<vector<int>> &arrs) {
vector<ListNode *> res;
res.reserve(arrs.size());
for (const auto &arr : arrs)
res.push_back(toListNode(arr));
return res;
}
inline TreeNode *toTreeNode(const vector<optional<int>> &arr) {
if (arr.empty() || !arr[0])
return nullptr;
TreeNode *root = new TreeNode(*arr[0]);
queue<TreeNode *> q;
q.push(root);
int i = 1;
while (!q.empty() && i < (int)arr.size()) {
TreeNode *node = q.front();
q.pop();
if (i < (int)arr.size() && arr[i]) {
node->left = new TreeNode(*arr[i]);
q.push(node->left);
}
i++;
if (i < (int)arr.size() && arr[i]) {
node->right = new TreeNode(*arr[i]);
q.push(node->right);
}
i++;
}
return root;
}
inline vector<optional<int>> treeNodeToArray(TreeNode *root) {
vector<optional<int>> res;
if (!root)
return res;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
TreeNode *node = q.front();
q.pop();
if (node) {
res.push_back(node->val);
q.push(node->left);
q.push(node->right);
} else {
res.push_back(nullopt);
}
}
while (!res.empty() && !res.back())
res.pop_back();
return res;
}
inline vector<vector<optional<int>>> treeNodesArrayToVector(const vector<TreeNode *> &roots) {
vector<vector<optional<int>>> res;
res.reserve(roots.size());
for (auto *root : roots) {
res.push_back(treeNodeToArray(root));
}
return res;
}
inline string _jsonQuote(const string &s) {
string r;
r.reserve(s.size() + 2);
r += '"';
for (unsigned char c : s) {
switch (c) {
case '"':
r += "\\\"";
break;
case '\\':
r += "\\\\";
break;
case '\b':
r += "\\b";
break;
case '\f':
r += "\\f";
break;
case '\n':
r += "\\n";
break;
case '\r':
r += "\\r";
break;
case '\t':
r += "\\t";
break;
default:
if (c < 0x20) {
char buf[8];
snprintf(buf, sizeof(buf), "\\u%04x", c);
r += buf;
} else {
r += (char)c;
}
}
}
r += '"';
return r;
}
inline string toJson(bool v) { return v ? "true" : "false"; }
inline string toJson(int v) { return to_string(v); }
inline string toJson(long v) { return to_string(v); }
inline string toJson(long long v) { return to_string(v); }
inline string toJson(unsigned v) { return to_string(v); }
inline string toJson(unsigned long v) { return to_string(v); }
inline string toJson(unsigned long long v) { return to_string(v); }
inline string toJson(double v) {
if (isnan(v) || isinf(v)) return "null";
ostringstream oss;
oss << setprecision(15) << v;
string s = oss.str();
if (s.find_first_of(".eE") == string::npos) s += ".0";
return s;
}
inline string toJson(float v) { return toJson((double)v); }
inline string toJson(char v) { return _jsonQuote(string(1, v)); }
inline string toJson(const string &v) { return _jsonQuote(v); }
inline string toJson(const char *v) { return _jsonQuote(string(v)); }
inline string toJson(nullptr_t) { return "null"; }
inline string toJson(const optional<int> &v) {
return v ? to_string(*v) : "null";
}
template <typename T> string toJson(const vector<T> &v) {
string r = "[";
for (int i = 0; i < (int)v.size(); i++) {
if (i)
r += ',';
r += toJson(v[i]);
}
r += ']';
return r;
}
inline string toJson(ListNode *head) { return toJson(listNodeToArray(head)); }
inline string toJson(TreeNode *root) { return toJson(treeNodeToArray(root)); }
class Solution {
public:
int addDigits(int n) {
if(n < 10)
return n;
return 1 + (n - 1) % 9 ;
}
};
int main() {
ifstream _TEST_JSON_FILE_("/src/test.json");
if (!_TEST_JSON_FILE_.is_open()) { return 1; }
string _TEST_JSON_STR_((istreambuf_iterator<char>(_TEST_JSON_FILE_)), istreambuf_iterator<char>());
_JSON_ _TEST_JSON_ = _JSON_::parse(_TEST_JSON_STR_, nullptr, false);
if (_TEST_JSON_.is_discarded()) { return 1; }
_JSON_ _RESULTS_ = _JSON_::array();
for (auto& caseJson : _TEST_JSON_) {
cout << "DEBUG:" << caseJson.dump() << endl;
auto inputs = caseJson["in"];
int _num = (inputs.contains("num") && inputs["num"].is_number()) ? inputs["num"].get<int>() : 0;
auto res = Solution().addDigits(_num);
_JSON_ resObj;
resObj = _JSON_::parse(toJson(res), nullptr, false);
if (resObj.is_discarded()) { resObj = toJson(res); }
_JSON_ resultItem;
resultItem["actual"] = resObj;
if (caseJson.contains("out")) {
resultItem["expected"] = caseJson["out"];
}
_RESULTS_.push_back(resultItem);
}
cout << "SF_JSON_SUMMARY_START\n" << _RESULTS_.dump() << "\nSF_JSON_SUMMARY_END\n";
return 0;
}