Skip to content

Commit b45693d

Browse files
committed
[ProblemSet] handle category in commands.
* show/submission: display category. * list: query problems by category. * fixes tiny bugs. Signed-off-by: Eric Wang <[email protected]>
1 parent c0ae81c commit b45693d

File tree

10 files changed

+39
-12
lines changed

10 files changed

+39
-12
lines changed

lib/cli.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var fs = require('fs');
22

3-
var sprintf = require('sprintf-js').sprintf;
43
var _ = require('underscore');
54

65
var h = require('./helper');

lib/commands/list.js

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ var cmd = {
2929
type: 'boolean',
3030
default: false,
3131
describe: 'Show problems statistics'
32+
},
33+
tag: {
34+
alias: 't',
35+
type: 'string',
36+
default: '',
37+
describe: 'Filter problems by tags'
3238
}
3339
}
3440
};
@@ -79,6 +85,12 @@ cmd.handler = function(argv) {
7985
});
8086
}
8187

88+
if (argv.tag) {
89+
problems = _.filter(problems, function(x) {
90+
return x.category === argv.tag;
91+
});
92+
}
93+
8294
var word = argv.keyword.toLowerCase();
8395
if (word) {
8496
if (word.endsWith(word.substr(-1).repeat(6))) {

lib/commands/show.js

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ cmd.handler = function(argv) {
8484
(problem.starred ? chalk.yellow(icon.like) : ' '),
8585
fileinfo));
8686
log.info(sprintf('%s\n', chalk.underline(problem.link)));
87+
log.info(sprintf('* %s', problem.category));
8788
log.info(sprintf('* %s (%.2f%%)', problem.level, problem.percent));
8889
log.info(sprintf('* Total Accepted: %s', problem.totalAC));
8990
log.info(sprintf('* Total Submissions: %s', problem.totalSubmit));

lib/config.js

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ var DEFAULT_SYS_CONFIG = {
3030
'ruby',
3131
'scala',
3232
'swift'
33+
],
34+
35+
CATEGORIES: [
36+
'algorithms',
37+
'database',
38+
'shell'
3339
]
3440
};
3541

lib/core.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ core.getProblems = function(cb) {
4343
}
4444

4545
var user = this.getUser();
46-
var CATEGORIES = ['algorithms', 'database', 'shell'];
4746
var KEY_TMP = '.tmp';
4847

4948
var doTask = function(category, taskDone) {
5049
log.debug(category + ': running getProblems');
5150
client.getProblems(category, user, function(e, problems) {
5251
if (e) {
53-
log.debug(category + ': failed to getProblems: ' + e);
52+
log.debug(category + ': failed to getProblems: ' + e.msg);
5453
} else {
5554
log.debug(category + ': getProblems got ' +
5655
problems.length + ' problems');
@@ -62,7 +61,7 @@ core.getProblems = function(cb) {
6261
};
6362

6463
cache.set(KEY_TMP, []);
65-
queue.run(CATEGORIES, doTask, function(e) {
64+
queue.run(config.CATEGORIES, doTask, function(e) {
6665
if (e) return cb(e);
6766

6867
saveUser(user);

lib/leetcode_client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function relogin(opts, cb) {
5858

5959
core.login(user, function(e, user) {
6060
if (e) {
61-
log.debug('login failed:' + e);
61+
log.debug('login failed:' + e.msg);
6262
} else {
6363
log.debug('login successfully, cont\'d...');
6464
signOpts(opts, user);

lib/queue.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var _ = require('underscore');
2+
13
var config = require('./config');
24

35
var queue = {};
@@ -21,7 +23,7 @@ function workerRun(ctx) {
2123

2224
queue.run = function(tasks, doTask, cb) {
2325
var ctx = {
24-
tasks: tasks,
26+
tasks: _.clone(tasks),
2527
doTask: doTask,
2628
cb: cb,
2729
workers: config.MAX_WORKERS || 1,

source.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<%= commentLine %>
44
<%= commentLine %> <%= link %>
55
<%= commentLine %>
6+
<%= commentLine %> <%= category %>
67
<%= commentLine %> <%= level %> (<%= percent %>%)
78
<%= commentLine %> Total Accepted: <%= totalAC %>
89
<%= commentLine %> Total Submissions: <%= totalSubmit %>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"state":"ac","id":2,"name":"Add Two Numbers","key":"add-two-numbers","link":"https://leetcode.com/problems/add-two-numbers","locked":false,"percent":25.368142876074806,"level":"Medium","starred":true,"totalAC":"195263","totalSubmit":"769711","desc":"You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.\r\n\r\nInput: (2 -> 4 -> 3) + (5 -> 6 -> 4)\r\nOutput: 7 -> 0 -> 8","templates":[{"value":"cpp","text":"C++","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * struct ListNode {\r\n * int val;\r\n * ListNode *next;\r\n * ListNode(int x) : val(x), next(NULL) {}\r\n * };\r\n */\r\nclass Solution {\r\npublic:\r\n ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\r\n \r\n }\r\n};"},{"value":"java","text":"Java","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * int val;\r\n * ListNode next;\r\n * ListNode(int x) { val = x; }\r\n * }\r\n */\r\npublic class Solution {\r\n public ListNode addTwoNumbers(ListNode l1, ListNode l2) {\r\n \r\n }\r\n}"},{"value":"python","text":"Python","defaultCode":"# Definition for singly-linked list.\r\n# class ListNode(object):\r\n# def __init__(self, x):\r\n# self.val = x\r\n# self.next = None\r\n\r\nclass Solution(object):\r\n def addTwoNumbers(self, l1, l2):\r\n \"\"\"\r\n :type l1: ListNode\r\n :type l2: ListNode\r\n :rtype: ListNode\r\n \"\"\"\r\n "},{"value":"c","text":"C","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * struct ListNode {\r\n * int val;\r\n * struct ListNode *next;\r\n * };\r\n */\r\nstruct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {\r\n \r\n}"},{"value":"csharp","text":"C#","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * public int val;\r\n * public ListNode next;\r\n * public ListNode(int x) { val = x; }\r\n * }\r\n */\r\npublic class Solution {\r\n public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {\r\n \r\n }\r\n}"},{"value":"javascript","text":"JavaScript","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * function ListNode(val) {\r\n * this.val = val;\r\n * this.next = null;\r\n * }\r\n */\r\n/**\r\n * @param {ListNode} l1\r\n * @param {ListNode} l2\r\n * @return {ListNode}\r\n */\r\nvar addTwoNumbers = function(l1, l2) {\r\n \r\n};"},{"value":"ruby","text":"Ruby","defaultCode":"# Definition for singly-linked list.\r\n# class ListNode\r\n# attr_accessor :val, :next\r\n# def initialize(val)\r\n# @val = val\r\n# @next = nil\r\n# end\r\n# end\r\n\r\n# @param {ListNode} l1\r\n# @param {ListNode} l2\r\n# @return {ListNode}\r\ndef add_two_numbers(l1, l2)\r\n \r\nend"},{"value":"swift","text":"Swift","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * public var val: Int\r\n * public var next: ListNode?\r\n * public init(_ val: Int) {\r\n * self.val = val\r\n * self.next = nil\r\n * }\r\n * }\r\n */\r\nclass Solution {\r\n func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {\r\n \r\n }\r\n}"},{"value":"golang","text":"Go","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * type ListNode struct {\r\n * Val int\r\n * Next *ListNode\r\n * }\r\n */\r\nfunc addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {\r\n \r\n}"}],"testcase":"[2,4,3]\n[5,6,4]","testable":true,"code":"class Solution {\r\npublic:\r\n ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\r\n\r\n }\r\n};"}
1+
{"state":"ac","id":2,"category":"algorithms","name":"Add Two Numbers","key":"add-two-numbers","link":"https://leetcode.com/problems/add-two-numbers","locked":false,"percent":25.368142876074806,"level":"Medium","starred":true,"totalAC":"195263","totalSubmit":"769711","desc":"You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.\r\n\r\nInput: (2 -> 4 -> 3) + (5 -> 6 -> 4)\r\nOutput: 7 -> 0 -> 8","templates":[{"value":"cpp","text":"C++","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * struct ListNode {\r\n * int val;\r\n * ListNode *next;\r\n * ListNode(int x) : val(x), next(NULL) {}\r\n * };\r\n */\r\nclass Solution {\r\npublic:\r\n ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\r\n \r\n }\r\n};"},{"value":"java","text":"Java","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * int val;\r\n * ListNode next;\r\n * ListNode(int x) { val = x; }\r\n * }\r\n */\r\npublic class Solution {\r\n public ListNode addTwoNumbers(ListNode l1, ListNode l2) {\r\n \r\n }\r\n}"},{"value":"python","text":"Python","defaultCode":"# Definition for singly-linked list.\r\n# class ListNode(object):\r\n# def __init__(self, x):\r\n# self.val = x\r\n# self.next = None\r\n\r\nclass Solution(object):\r\n def addTwoNumbers(self, l1, l2):\r\n \"\"\"\r\n :type l1: ListNode\r\n :type l2: ListNode\r\n :rtype: ListNode\r\n \"\"\"\r\n "},{"value":"c","text":"C","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * struct ListNode {\r\n * int val;\r\n * struct ListNode *next;\r\n * };\r\n */\r\nstruct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {\r\n \r\n}"},{"value":"csharp","text":"C#","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * public int val;\r\n * public ListNode next;\r\n * public ListNode(int x) { val = x; }\r\n * }\r\n */\r\npublic class Solution {\r\n public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {\r\n \r\n }\r\n}"},{"value":"javascript","text":"JavaScript","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * function ListNode(val) {\r\n * this.val = val;\r\n * this.next = null;\r\n * }\r\n */\r\n/**\r\n * @param {ListNode} l1\r\n * @param {ListNode} l2\r\n * @return {ListNode}\r\n */\r\nvar addTwoNumbers = function(l1, l2) {\r\n \r\n};"},{"value":"ruby","text":"Ruby","defaultCode":"# Definition for singly-linked list.\r\n# class ListNode\r\n# attr_accessor :val, :next\r\n# def initialize(val)\r\n# @val = val\r\n# @next = nil\r\n# end\r\n# end\r\n\r\n# @param {ListNode} l1\r\n# @param {ListNode} l2\r\n# @return {ListNode}\r\ndef add_two_numbers(l1, l2)\r\n \r\nend"},{"value":"swift","text":"Swift","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * public var val: Int\r\n * public var next: ListNode?\r\n * public init(_ val: Int) {\r\n * self.val = val\r\n * self.next = nil\r\n * }\r\n * }\r\n */\r\nclass Solution {\r\n func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {\r\n \r\n }\r\n}"},{"value":"golang","text":"Go","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * type ListNode struct {\r\n * Val int\r\n * Next *ListNode\r\n * }\r\n */\r\nfunc addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {\r\n \r\n}"}],"testcase":"[2,4,3]\n[5,6,4]","testable":true,"code":"class Solution {\r\npublic:\r\n ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\r\n\r\n }\r\n};"}

test/test_core.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,29 @@ var config = rewire('../lib/config');
1414
var core = rewire('../lib/core');
1515
var h = rewire('../lib/helper');
1616

17+
var HOME = './tmp';
18+
1719
describe('core', function() {
1820
before(function() {
1921
log.init();
2022

21-
var home = './tmp';
22-
execSync('rm -rf ' + home);
23-
fs.mkdirSync(home);
24-
2523
h.getHomeDir = function() {
26-
return home;
24+
return HOME;
2725
};
2826

27+
config.CATEGORIES = ['algorithms', 'database', 'shell'];
28+
2929
cache.__set__('h', h);
3030
core.__set__('cache', cache);
3131
core.__set__('client', client);
3232
core.__set__('config', config);
3333
});
3434

35+
beforeEach(function() {
36+
execSync('rm -rf ' + HOME);
37+
fs.mkdirSync(HOME);
38+
});
39+
3540
describe('#user', function() {
3641
var USER = {name: 'test-user', pass: 'password'};
3742
var USER_AFTER = {name: 'test-user', pass: 'password', hash: 'abcdef'};
@@ -374,6 +379,7 @@ describe('core', function() {
374379
' *',
375380
' * https://leetcode.com/problems/add-two-numbers',
376381
' *',
382+
' * algorithms',
377383
' * Medium (25.37%)',
378384
' * Total Accepted: 195263',
379385
' * Total Submissions: 769711',
@@ -408,6 +414,7 @@ describe('core', function() {
408414
'#',
409415
'# https://leetcode.com/problems/add-two-numbers',
410416
'#',
417+
'# algorithms',
411418
'# Medium (25.37%)',
412419
'# Total Accepted: 195263',
413420
'# Total Submissions: 769711',

0 commit comments

Comments
 (0)