From 14011a00158b43b882bec085bfbb717ea56711da Mon Sep 17 00:00:00 2001 From: Zhang Xu Date: Tue, 16 Apr 2019 15:57:19 +0800 Subject: [PATCH 1/4] sort by percent --- lib/commands/list.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/commands/list.js b/lib/commands/list.js index c010de86..25217ea1 100644 --- a/lib/commands/list.js +++ b/lib/commands/list.js @@ -59,7 +59,7 @@ cmd.handler = function(argv) { const stat = {}; for (let x of ['locked', 'starred', 'ac', 'notac', 'None', 'Easy', 'Medium', 'Hard']) stat[x] = 0; - problems = _.sortBy(problems, x => -x.fid); + problems = _.sortBy(problems, x => x.percent); for (let problem of problems) { stat[problem.level] = (stat[problem.level] || 0) + 1; stat[problem.state] = (stat[problem.state] || 0) + 1; From 858a12edf0830c20c9394a5f3e18c1e4e839de46 Mon Sep 17 00:00:00 2001 From: Zhang Xu Date: Tue, 16 Apr 2019 15:57:40 +0800 Subject: [PATCH 2/4] add plugin for golang --- lib/plugins/golang.run.js | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lib/plugins/golang.run.js diff --git a/lib/plugins/golang.run.js b/lib/plugins/golang.run.js new file mode 100644 index 00000000..49bdea20 --- /dev/null +++ b/lib/plugins/golang.run.js @@ -0,0 +1,74 @@ +var cp = require('child_process'); +var fs = require('fs'); + +var h = require('../helper'); +var log = require('../log'); +var Plugin = require('../plugin.js'); +var session = require('../session'); + +// Please note that we DON'T want implement a lightweight judge engine +// here, thus we are NOT going to support all the problems!!! +// +// Only works for those problems could be easily tested. +// +// [Usage] +// +// https://github.com/skygragon/leetcode-cli-plugins/blob/master/docs/cpp.run.md +// +var plugin = new Plugin(100, 'golang.run', '2017.07.29', + 'Plugin to run golang code locally for debugging.'); + +plugin.testProblem = function (problem, cb) { + let ori = null; + let tmp = null; + if (session.argv.tpl) { + log.info('Rewrite source code ' + problem.file); + let code = fs.readFileSync(problem.file).toString() + let strs = code.split('//--ignore--'); + code = strs[strs.length - 1] + ori = problem.file; + tmp = ori + '.tmp'; + fs.writeFileSync(tmp, code); + problem.file = tmp; + } + + let ans = plugin.next.testProblem(problem, cb); + if (session.argv.tpl) { + fs.unlinkSync(problem.file); + problem.file = ori; + } + return ans +}; + +plugin.submitProblem = function (problem, cb) { + let ori = null; + let tmp = null; + if (session.argv.tpl) { + log.info('Rewrite source code ' + problem.file); + let code = fs.readFileSync(problem.file).toString() + let strs = code.split('//--ignore--'); + code = strs[strs.length - 1] + ori = problem.file; + tmp = ori + '.tmp'; + fs.writeFileSync(tmp, code); + problem.file = tmp; + } + + let ans = plugin.next.submitProblem(problem, cb); + if (session.argv.tpl) { + fs.unlinkSync(problem.file); + problem.file = ori; + } + return ans +}; + +plugin.exportProblem = function (problem, opts) { + let header = ''; + if (session.argv.tpl) { + log.info('Rewrite source code ' + problem.file); + header = 'package main\n\n//--ignore--' + } + return header + plugin.next.exportProblem(problem, opts); +}; + +module.exports = plugin; From 40c9417239a4fd7841e916f1a0f09b7b953d6d8b Mon Sep 17 00:00:00 2001 From: Zhang Xu Date: Fri, 19 Apr 2019 14:33:54 +0800 Subject: [PATCH 3/4] add order & order by option on list command --- lib/commands/list.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/commands/list.js b/lib/commands/list.js index c010de86..be8cf2c9 100644 --- a/lib/commands/list.js +++ b/lib/commands/list.js @@ -12,7 +12,7 @@ const cmd = { command: 'list [keyword]', aliases: ['ls'], desc: 'List questions', - builder: function(yargs) { + builder: function(yargs) { return yargs .option('q', core.filters.query) .option('s', { @@ -28,6 +28,18 @@ const cmd = { default: false, describe: 'Show extra details: category, companies, tags.' }) + .option('o', { + alias: 'order', + default: 'asc', + describe: 'choose order', + choices: ['asc', 'desc'] + }) + .option('b', { + alias: 'order-by', + default: 'fid', + describe: 'choose order by', + choices: ['id', 'fid', 'percent'] + }) .positional('keyword', { type: 'string', default: '', @@ -59,7 +71,8 @@ cmd.handler = function(argv) { const stat = {}; for (let x of ['locked', 'starred', 'ac', 'notac', 'None', 'Easy', 'Medium', 'Hard']) stat[x] = 0; - problems = _.sortBy(problems, x => -x.fid); + const f = argv.o == 'asc' ? function(v){return v} : function(v) {return -v}; + problems = _.sortBy(problems, x => f(x[argv.b])); for (let problem of problems) { stat[problem.level] = (stat[problem.level] || 0) + 1; stat[problem.state] = (stat[problem.state] || 0) + 1; From c38a645478ecaa1ed89b92232597b5ef40954979 Mon Sep 17 00:00:00 2001 From: Zhang Xu Date: Fri, 19 Apr 2019 14:36:43 +0800 Subject: [PATCH 4/4] add a new beats data calculation --- lib/commands/submit.js | 14 ++++++++++++++ lib/plugins/leetcode.js | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/commands/submit.js b/lib/commands/submit.js index 7376598b..001d598e 100644 --- a/lib/commands/submit.js +++ b/lib/commands/submit.js @@ -67,6 +67,20 @@ cmd.handler = function(argv) { session.updateStat('ac', 1); session.updateStat('ac.set', problem.fid); core.getSubmission({id: result.id}, function(e, submission) { + if (submission && submission.runtimeDistributionFormatted && submission.runtimeDistributionFormatted.distribution) { + const d = submission.runtimeDistributionFormatted.distribution; + const runtime = parseInt(result.runtime.split(' ')[0]); + let b = 0; + for (let i = 0 ; i < d.length;i++){ + if (d[i][0] >= runtime){ + break; + } + b += d[i][1]; + } + b = 100 - b; + printLine(result, 'Your runtime beats %d %% of %s submissions', b, submission.runtimeDistributionFormatted.lang); + return; + } if (e || !submission || !submission.distributionChart) return log.warn('Failed to get submission beat ratio.'); diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 0ce3a66e..072b6a18 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -336,6 +336,15 @@ plugin.getSubmission = function(submission, cb) { let re = body.match(/submissionCode:\s('[^']*')/); if (re) submission.code = eval(re[1]); + re = body.match(/runtimeDistributionFormatted:\s('[^']+')/); + if (re) submission.runtimeDistributionFormatted = JSON.parse(eval(re[1])); + re = body.match(/memoryDistributionFormatted:\s('[^']+')/); + if (re) submission.memoryDistributionFormatted = JSON.parse(eval(re[1])); + re = body.match(/runtime:\s'([^']+)'/); + if (re) submission.runtime = re[1]; + re = body.match(/memory:\s'([^']+)'/); + if (re) submission.memory = re[1]; + re = body.match(/distribution_formatted:\s('[^']+')/); if (re) submission.distributionChart = JSON.parse(eval(re[1])); return cb(null, submission);