Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 887996a

Browse files
committed
Enhance param deduction in cpp.run
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 8c4c390 commit 887996a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

cpp.run.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,18 @@ plugin.testProblem = function(problem, cb) {
2828

2929
// generate full cpp source code that runnable
3030
var meta = problem.templateMeta;
31-
var args = problem.testcase.split('\n').map(function(x, i) {
31+
32+
var code = fs.readFileSync(problem.file).toString();
33+
var re = code.match(new RegExp(' ' + meta.name + '\\((.+)\\)'));
34+
if (!re) return cb('failed to generate runnable code!');
35+
36+
var types = re[1].split(',').map(function(x) {
37+
var parts = x.trim().split(' ');
38+
parts.pop(); // skip param name
39+
return parts.join(' ');
40+
});
41+
42+
var values = problem.testcase.split('\n').map(function(x, i) {
3243
// TODO: handle more special types??
3344
// array, list, tree, etc
3445
var t = meta.params[i].type;
@@ -40,9 +51,15 @@ plugin.testProblem = function(problem, cb) {
4051
return x;
4152
});
4253

43-
var data = DATA.replace('$code', fs.readFileSync(problem.file))
54+
var data = DATA.replace('$code', code)
4455
.replace('$method', meta.name)
45-
.replace('$args', args.join(','));
56+
.replace('$argDefs', values.map(function(x, i) {
57+
return ' decay<' + types[i] + '>::type ' + 'p' + i + ' = ' + x + ';';
58+
}).join('\n'))
59+
.replace('$args', values.map(function(x, i) {
60+
return 'p' + i;
61+
}).join(','));
62+
4663
fs.writeFileSync(FILE_SRC, data);
4764

4865
// compile and run
@@ -197,6 +214,7 @@ ostream& operator<<(ostream &os, const TreeNode *t) {
197214
$code
198215
int main() {
199216
Solution s;
217+
$argDefs
200218
auto res = s.$method($args);
201219
cout << res << endl;
202220
return 0;

0 commit comments

Comments
 (0)