Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c59addb

Browse files
refs leetcode-tools#67: add -q -t to randomly show
* reuse the same options as `list` Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 7236c60 commit c59addb

File tree

4 files changed

+134
-119
lines changed

4 files changed

+134
-119
lines changed

‎lib/commands/list.js‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,14 @@ var cmd = {
1414
desc: 'List questions',
1515
builder: function(yargs) {
1616
return yargs
17-
.option('q', {
18-
alias: 'query',
19-
type: 'string',
20-
default: '',
21-
describe: [
22-
'Filter questions by condition:',
23-
'Uppercase means negative',
24-
'e = easy E = m+h',
25-
'm = medium M = e+h',
26-
'h = hard H = e+m',
27-
'd = done D = not done',
28-
'l = locked L = non locked',
29-
's = starred S = not starred'
30-
].join('\n')
31-
})
17+
.option('q', core.filters.query)
3218
.option('s', {
3319
alias: 'stat',
3420
type: 'boolean',
3521
default: false,
3622
describe: 'Show statistics of listed questions'
3723
})
38-
.option('t', {
39-
alias: 'tag',
40-
type: 'array',
41-
default: [],
42-
describe: 'Filter questions by tag'
43-
})
24+
.option('t', core.filters.tag)
4425
.positional('keyword', {
4526
type: 'string',
4627
default: '',

‎lib/commands/show.js‎

Lines changed: 99 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ var cmd = {
4242
describe: 'Programming language of the source code',
4343
choices: config.sys.langs
4444
})
45+
.option('q', core.filters.query)
46+
.option('t', core.filters.tag)
4547
.option('x', {
4648
alias: 'extra',
4749
type: 'boolean',
@@ -54,9 +56,12 @@ var cmd = {
5456
describe: 'Show question by name or id'
5557
})
5658
.example(chalk.yellow('leetcode show 1'), 'Show question 1')
57-
.example(chalk.yellow('leetcode show'), 'Show random question')
5859
.example(chalk.yellow('leetcode show 1 -gx -l java'), 'Show question 1 and generate Java code')
59-
.example(chalk.yellow('leetcode show 1 -gxe'), 'Open generated code in editor');
60+
.example(chalk.yellow('leetcode show 1 -gxe'), 'Open generated code in editor')
61+
.example('', '')
62+
.example(chalk.yellow('leetcode show'), 'Show random question')
63+
.example(chalk.yellow('leetcode show -q h'), 'Show random hard question')
64+
.example(chalk.yellow('leetcode show -t google'), 'Show random question from Google (require plugin)');
6065
}
6166
};
6267

@@ -69,76 +74,102 @@ function genFileName(problem, lang) {
6974
return name;
7075
}
7176

72-
cmd.handler = function(argv) {
73-
session.argv = argv;
74-
core.getProblem(argv.keyword, function(e, problem) {
75-
if (e) return log.fail(e);
76-
77-
var langlist = problem.templates
78-
.map(function(x) { return x.value; })
79-
.sort()
80-
.join(', ');
81-
82-
var code;
83-
var needcode = argv.gen || argv.codeonly;
84-
if (needcode) {
85-
var template = _.find(problem.templates, function(x) {
86-
return x.value === argv.lang;
87-
});
88-
if (!template) {
89-
log.fail('Not supported language "' + argv.lang + '"');
90-
log.warn('Supported languages: ' + langlist);
91-
return;
92-
}
93-
94-
var opts = {
95-
lang: argv.lang,
96-
code: template.defaultCode,
97-
tpl: argv.extra ? 'detailed' : 'codeonly'
98-
};
99-
code = core.exportProblem(problem, opts);
77+
function showProblem(problem, argv) {
78+
var langlist = problem.templates
79+
.map(function(x) { return x.value; })
80+
.sort()
81+
.join(', ');
82+
83+
var code;
84+
var needcode = argv.gen || argv.codeonly;
85+
if (needcode) {
86+
var template = _.find(problem.templates, function(x) {
87+
return x.value === argv.lang;
88+
});
89+
if (!template) {
90+
log.fail('Not supported language "' + argv.lang + '"');
91+
log.warn('Supported languages: ' + langlist);
92+
return;
10093
}
10194

102-
var filename;
103-
if (argv.gen) {
104-
filename = genFileName(problem, argv.lang);
105-
fs.writeFileSync(filename, code);
106-
107-
if (argv.editor !== undefined) {
108-
childProcess.spawn(argv.editor || config.code.editor, [filename], {
109-
// in case your editor of choice is vim or emacs
110-
stdio: 'inherit'
111-
});
112-
}
113-
} else {
114-
if (argv.codeonly) {
115-
log.info(chalk.yellow(code));
116-
return;
117-
}
95+
var opts = {
96+
lang: argv.lang,
97+
code: template.defaultCode,
98+
tpl: argv.extra ? 'detailed' : 'codeonly'
99+
};
100+
code = core.exportProblem(problem, opts);
101+
}
102+
103+
var filename;
104+
if (argv.gen) {
105+
filename = genFileName(problem, argv.lang);
106+
fs.writeFileSync(filename, code);
107+
108+
if (argv.editor !== undefined) {
109+
childProcess.spawn(argv.editor || config.code.editor, [filename], {
110+
// in case your editor of choice is vim or emacs
111+
stdio: 'inherit'
112+
});
113+
}
114+
} else {
115+
if (argv.codeonly) {
116+
log.info(chalk.yellow(code));
117+
return;
118118
}
119+
}
119120

120-
log.printf('[%d] %s %s', problem.id, problem.name,
121-
(problem.starred ? chalk.yellow(icon.like) : icon.none));
122-
log.info();
123-
log.info(chalk.underline(problem.link));
124-
125-
log.info();
126-
log.printf('* %s', problem.category);
127-
log.printf('* %s (%.2f%%)', h.prettyLevel(problem.level), problem.percent);
128-
129-
if (filename)
130-
log.printf('* Source Code: %s', chalk.yellow.underline(filename));
131-
if (problem.totalAC)
132-
log.printf('* Total Accepted: %s', problem.totalAC);
133-
if (problem.totalSubmit)
134-
log.printf('* Total Submissions: %s', problem.totalSubmit);
135-
if (problem.testable && problem.testcase)
136-
log.printf('* Testcase Example: %s', chalk.yellow(util.inspect(problem.testcase)));
137-
log.printf('* Avail Languages: %s', langlist);
138-
139-
log.info();
140-
log.info(problem.desc);
141-
});
121+
log.printf('[%d] %s %s', problem.id, problem.name,
122+
(problem.starred ? chalk.yellow(icon.like) : icon.none));
123+
log.info();
124+
log.info(chalk.underline(problem.link));
125+
126+
log.info();
127+
log.printf('* %s', problem.category);
128+
log.printf('* %s (%.2f%%)', h.prettyLevel(problem.level), problem.percent);
129+
130+
if (filename)
131+
log.printf('* Source Code: %s', chalk.yellow.underline(filename));
132+
if (problem.totalAC)
133+
log.printf('* Total Accepted: %s', problem.totalAC);
134+
if (problem.totalSubmit)
135+
log.printf('* Total Submissions: %s', problem.totalSubmit);
136+
if (problem.testable && problem.testcase)
137+
log.printf('* Testcase Example: %s', chalk.yellow(util.inspect(problem.testcase)));
138+
log.printf('* Avail Languages: %s', langlist);
139+
140+
log.info();
141+
log.info(problem.desc);
142+
}
143+
144+
cmd.handler = function(argv) {
145+
session.argv = argv;
146+
if (argv.keyword.length > 0) {
147+
// show specific one
148+
core.getProblem(argv.keyword, function(e, problem) {
149+
if (e) return log.fail(e);
150+
showProblem(problem, argv);
151+
});
152+
} else {
153+
// show random one
154+
core.filterProblems(argv, function(e, problems) {
155+
if (e) return log.fail(e);
156+
157+
// random select one that not AC-ed yet
158+
var user = session.getUser();
159+
problems = _.filter(problems, function(x) {
160+
if (x.state === 'ac') return false;
161+
if (!user.paid && x.locked) return false;
162+
return true;
163+
});
164+
if (problems.length === 0) return cb('Problem not found!');
165+
166+
var problem = problems[_.random(problems.length - 1)];
167+
core.getProblem(problem, function(e, problem) {
168+
if (e) return log.fail(e);
169+
showProblem(problem, argv);
170+
});
171+
});
172+
}
142173
};
143174

144175
module.exports = cmd;

‎lib/core.js‎

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,33 @@ var _ = require('underscore');
66
var log = require('./log');
77
var h = require('./helper');
88
var Plugin = require('./plugin');
9-
var session = require('./session');
109

1110
var core = new Plugin(99999999, 'core', '20170722', 'Plugins manager');
1211

12+
core.filters = {
13+
query: {
14+
alias: 'query',
15+
type: 'string',
16+
default: '',
17+
describe: [
18+
'Filter questions by condition:',
19+
'Uppercase means negative',
20+
'e = easy E = m+h',
21+
'm = medium M = e+h',
22+
'h = hard H = e+m',
23+
'd = done D = not done',
24+
'l = locked L = non locked',
25+
's = starred S = not starred'
26+
].join('\n')
27+
},
28+
tag: {
29+
alias: 'tag',
30+
type: 'array',
31+
default: [],
32+
describe: 'Filter questions by tag'
33+
}
34+
};
35+
1336
function hasTag(o, tag) {
1437
return _.isArray(o) &&
1538
_.some(o, function(x) { return x.indexOf(tag.toLowerCase()) >= 0; });
@@ -58,28 +81,16 @@ core.filterProblems = function(opts, cb) {
5881
};
5982

6083
core.getProblem = function(keyword, cb) {
84+
if (keyword.id)
85+
return core.next.getProblem(keyword, cb);
86+
6187
this.getProblems(function(e, problems) {
6288
if (e) return cb(e);
6389

64-
var problem;
6590
keyword = Number(keyword) || keyword;
66-
67-
if (keyword === '') {
68-
var user = session.getUser();
69-
// random select one that not AC-ed yet
70-
problems = _.filter(problems, function(x) {
71-
if (x.state === 'ac') return false;
72-
if (!user.paid && x.locked) return false;
73-
return true;
74-
});
75-
if (problems.length > 0)
76-
problem = problems[_.random(problems.length - 1)];
77-
} else {
78-
problem = _.find(problems, function(x) {
79-
return x.id === keyword || x.name === keyword || x.slug === keyword;
80-
});
81-
}
82-
91+
var problem = _.find(problems, function(x) {
92+
return x.id === keyword || x.name === keyword || x.slug === keyword;
93+
});
8394
if (!problem) return cb('Problem not found!');
8495
core.next.getProblem(problem, cb);
8596
});

‎test/test_core.js‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -297,18 +297,10 @@ describe('core', function() {
297297
});
298298
});
299299

300-
it('should getProblem random ok', function(done) {
301-
NEXT.getProblems = function(cb) {
302-
return cb(null, [
303-
{id: 0, state: 'ac', locked: false},
304-
{id: 1, state: 'none', locked: true},
305-
{id: 2, state: 'none', locked: false}
306-
]);
307-
};
308-
309-
plugin.getProblem('', function(e, problem) {
300+
it('should getProblem ok if problem is already there', function(done) {
301+
plugin.getProblem(PROBLEMS[1], function(e, problem) {
310302
assert.equal(e, null);
311-
assert.equal(problem.id,2);
303+
assert.deepEqual(problem,PROBLEMS[1]);
312304
done();
313305
});
314306
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /