From bdd6297c8dde2dfca96168a4edce5707c6438e02 Mon Sep 17 00:00:00 2001 From: Fan Zhenya Date: Tue, 27 Jun 2017 00:59:25 +0800 Subject: [PATCH] Add beat ratio display after AC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sample output: ✔ Accepted ✔ 1562/1562 cases passed (53 ms) ✔ Your runtime beats 74.21 % of java submissions Signed-off-by: Fan Zhenya --- lib/commands/submit.js | 22 ++++++++++++++++++++++ lib/leetcode_client.js | 5 +++++ test/test_leetcode_client.js | 8 ++++---- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/commands/submit.js b/lib/commands/submit.js index 2e7f2155..fc155a4b 100644 --- a/lib/commands/submit.js +++ b/lib/commands/submit.js @@ -54,6 +54,28 @@ cmd.handler = function(argv) { result.status_runtime); log.info(INDENT + h.prettyText(line, ok)); + // show beat ratio + if (ok) { + core.getSubmission({id: result.id}, function(e, submission) { + if (submission.distributionChart) { + var beatRatio = 0.0; + var chart = submission.distributionChart.distribution; + var lang = submission.distributionChart.lang; + for (var i in chart) { + if (parseFloat(chart[i][0]) <= parseFloat(result.display_runtime)) { + continue; + } else { + beatRatio += parseFloat(chart[i][1]); + } + } + line = util.format(' Your runtime beats %d %% of %s submissions', + beatRatio.toFixed(2), + lang); + log.info(INDENT + h.prettyText(line, ok)); + } + }); + } + // show testcase var testcase = result.input || result.last_testcase; if (!ok && testcase) { diff --git a/lib/leetcode_client.js b/lib/leetcode_client.js index 5fef30f4..a7fd128f 100644 --- a/lib/leetcode_client.js +++ b/lib/leetcode_client.js @@ -209,6 +209,10 @@ leetcodeClient.getSubmission = function(submission, cb) { if (re) { submission.code = eval(re[1]); } + re = body.match(/distribution_formatted:\s('[^']*')/); + if (re) { + submission.distributionChart = JSON.parse(eval(re[1])); + } return cb(null, submission); }); }; @@ -273,6 +277,7 @@ function verifyResult(opts, jobs, results, cb) { var result = JSON.parse(body); if (result.state === 'SUCCESS') { result.name = jobs[0].name; + result.id = jobs[0].id; results.push(result); jobs.shift(); } diff --git a/test/test_leetcode_client.js b/test/test_leetcode_client.js index bc7efc74..5bd5482b 100644 --- a/test/test_leetcode_client.js +++ b/test/test_leetcode_client.js @@ -280,8 +280,8 @@ describe('leetcode_client', function() { assert.equal(e, null); assert.deepEqual(results, [ - {name: 'Your', state: 'SUCCESS'}, - {name: 'Expected', state: 'SUCCESS'} + {id: 'id2', name: 'Your', state: 'SUCCESS'}, + {id: 'id1', name: 'Expected', state: 'SUCCESS'} ]); done(); }); @@ -311,7 +311,7 @@ describe('leetcode_client', function() { client.submitProblem(PROBLEM, function(e, results) { assert.equal(e, null); - assert.deepEqual(results, [{name: 'Your', state: 'SUCCESS'}]); + assert.deepEqual(results, [{id: 'id1', name: 'Your', state: 'SUCCESS'}]); done(); }); }); @@ -335,7 +335,7 @@ describe('leetcode_client', function() { client.submitProblem(PROBLEM, function(e, results) { assert.equal(e, null); - assert.deepEqual(results, [{name: 'Your', state: 'SUCCESS'}]); + assert.deepEqual(results, [{id: 'id1', name: 'Your', state: 'SUCCESS'}]); done(); }); });