diff --git a/lib/commands/list.js b/lib/commands/list.js index c010de86..6ad9b43d 100644 --- a/lib/commands/list.js +++ b/lib/commands/list.js @@ -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; diff --git a/lib/commands/submit.js b/lib/commands/submit.js index 56f5ed04..ba37cb92 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/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; diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 24331ec6..fae1cef2 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -343,6 +343,17 @@ plugin.getSubmission = function(submission, cb) { 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); });