irongit

A Neovim plugin enabling you to solve LeetCode problems.

48 lines1.2 KBLua
1local log = require("leetcode.logger")
2
3local cn_utils = {}
4
5---@param str string
6local function capitalizeFirst(str)
7 return str:lower():gsub("^%l", string.upper)
8end
9
10---@return table
11function cn_utils.calc_question_count(stats)
12 ---@type table<string, { all: integer, solved: integer }>
13 local lvls = {
14 All = {},
15 Easy = {},
16 Medium = {},
17 Hard = {},
18 }
19
20 local all = lvls["All"]
21 for key, value in pairs(stats) do
22 for _, count in ipairs(value) do
23 local diff = count.difficulty
24 local lvl = lvls[capitalizeFirst(diff)]
25
26 all.all = (all.all or 0) + count.count
27 lvl.all = (lvl.all or 0) + count.count
28
29 if key == "numAcceptedQuestions" then
30 all.solved = (all.solved or 0) + count.count
31 lvl.solved = (lvl.solved or 0) + count.count
32 end
33 end
34 end
35
36 local ac_count, all_count = {}, {}
37 for key, lvl in pairs(lvls) do
38 table.insert(ac_count, { difficulty = key, count = lvl.solved })
39 table.insert(all_count, { difficulty = key, count = lvl.all })
40 end
41
42 return {
43 submit_stats = { acSubmissionNum = ac_count },
44 questions_count = all_count,
45 }
46end
47
48return cn_utils