-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolution.go
369 lines (315 loc) · 8.61 KB
/
solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package command
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/olekukonko/tablewriter"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/spf13/cobra"
"github.com/gladmo/leetcode/leet"
"github.com/gladmo/leetcode/store"
)
var solutionCmd = &cobra.Command{
Use: "solution",
Short: "题解管理",
Long: "你测试过的题解都可以被查看,还可以将之前提交的代码检出到问题列表中",
ValidArgs: []string{"get", "describe", "code", "checkout", "list", "diff"},
}
func init() {
solutionCmd.AddCommand(
solutionListCmd,
solutionGetCmd,
solutionDescribeCmd,
solutionCodeCmd,
solutionCheckoutCmd,
solutionDiffCmd,
solutionTidyCmd,
solutionEmptyCmd,
)
}
const timeFormat = "2006-01-02"
var solutionListCmd = &cobra.Command{
Use: "list",
Short: "列出所有你测试过的题解",
Run: func(cmd *cobra.Command, args []string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetRowLine(true)
table.SetHeader([]string{"问题ID", "标题", "难度", "版本数", "测试次数", "首次提交", "最后提交"})
list, err := store.ListSolution()
if err != nil {
fmt.Println(err.Error())
return
}
for _, solution := range list {
info := leet.GetQuestionInfo(leet.Parse(solution.QuestionID))
table.Append([]string{
info.QuestionID,
info.TranslatedTitle,
info.Difficulty,
fmt.Sprint(solution.Version),
fmt.Sprint(solution.Times),
solution.FirstTime.Format(timeFormat),
solution.LastTime.Format(timeFormat),
})
}
table.Render()
},
}
var solutionGetCmd = &cobra.Command{
Use: "get question_id|leetcode_url",
Short: "获取某一题目的题解列表",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Println("参数异常")
cmd.Help()
os.Exit(1)
return
}
titleSlug := leet.Parse(strings.TrimSpace(args[0]))
info := leet.GetQuestionInfo(titleSlug)
solutions, err := store.GetSolution(info.QuestionID)
if err != nil {
fmt.Println(err.Error())
return
}
table := tablewriter.NewWriter(os.Stdout)
table.SetRowLine(true)
// table.SetAutoMergeCells(true)
table.SetHeader([]string{"序号", "语言", "测试次数", "评价", "消耗", "创建时间", "备注"})
for idx, solution := range solutions {
table.Append([]string{
fmt.Sprint(idx + 1),
solution.Language,
fmt.Sprint(solution.Times),
solution.Evaluation,
fmt.Sprintf("%v", solution.Consumption-(solution.Consumption%time.Millisecond)),
solution.CreatedAt.Format(timeFormat),
solution.Remark,
})
}
table.Render()
},
}
var solutionCodeCmd = &cobra.Command{
Use: "code question_id|leetcode_url [solution_no]",
Short: "显示你的题解代码",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 || len(args) > 2 {
cmd.Println("参数异常")
cmd.Help()
os.Exit(1)
return
}
// 展示最后一个
var latest bool
var idx int
var err error
if len(args) == 1 {
latest = true
} else {
solutionIndex := strings.TrimSpace(args[1])
idx, err = strconv.Atoi(solutionIndex)
if err != nil {
latest = true
}
// 转换成数组索引
idx--
}
titleSlug := leet.Parse(strings.TrimSpace(args[0]))
info := leet.GetQuestionInfo(titleSlug)
solutions, err := store.GetSolution(info.QuestionID)
if err != nil {
fmt.Println(err.Error())
return
}
if latest || idx > len(solutions) {
idx = len(solutions) - 1
}
solution := solutions[idx]
fmt.Println(leet.CustomerCode(solution.Code))
},
}
var solutionCheckoutCmd = &cobra.Command{
Use: "checkout question_id|leetcode_url solution_no",
Short: "将题解恢复到 questions 目录中,可以使用test重新测试",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
cmd.Println("参数异常")
cmd.Help()
os.Exit(1)
return
}
titleSlug := leet.Parse(strings.TrimSpace(args[0]))
solutionIndex := strings.TrimSpace(args[1])
idx, err := strconv.Atoi(solutionIndex)
if err != nil {
fmt.Println(err.Error())
return
}
idx--
info := leet.GetQuestionInfo(titleSlug)
solutions, err := store.GetSolution(info.QuestionID)
if err != nil {
fmt.Println(err.Error())
return
}
if idx < 0 || idx >= len(solutions) {
idx++
fmt.Println(fmt.Sprintf("最大题解数为: %d, 当前输入: %d", len(solutions), idx))
return
}
solution := solutions[idx]
err = ioutil.WriteFile(solution.SourceDir, []byte(solution.Code), 0755)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(fmt.Sprintf(
"题目: %s, 编号: %s, 代码检出成功。",
info.TranslatedTitle,
info.QuestionID,
))
codeLine := fmt.Sprintf("%s:%d", solution.SourceDir, leet.CustomerCodeLine(solution.Code))
fmt.Println(fmt.Sprintf("路径: %s", codeLine))
},
}
var solutionDescribeCmd = &cobra.Command{
Use: "describe question_id|leetcode_url solution_no",
Short: "详细描述你的题解内容",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
cmd.Println("参数异常")
cmd.Help()
os.Exit(1)
return
}
titleSlug := leet.Parse(strings.TrimSpace(args[0]))
solutionIndex := strings.TrimSpace(args[1])
idx, err := strconv.Atoi(solutionIndex)
if err != nil {
fmt.Println(err.Error())
return
}
// 转为数组索引
idx--
info := leet.GetQuestionInfo(titleSlug)
solutions, err := store.GetSolution(info.QuestionID)
if err != nil {
fmt.Println(err)
return
}
if idx < 0 || idx >= len(solutions) {
idx++
fmt.Println(fmt.Sprintf("最大题解数为: %d, 当前输入: %d", len(solutions), idx))
return
}
solution := solutions[idx]
code := leet.CustomerCode(solution.Code)
table := tablewriter.NewWriter(os.Stdout)
table.SetRowLine(true)
table.Append([]string{"编号:", info.QuestionID})
table.Append([]string{"题目:", info.TranslatedTitle})
table.Append([]string{"难度:", info.Difficulty})
table.Append([]string{"语言:", solution.Language})
codeLine := fmt.Sprintf("%s:%d", solution.SourceDir, leet.CustomerCodeLine(solution.Code))
table.Append([]string{"路径:", codeLine})
table.Append([]string{"时间:", solution.CreatedAt.Format(timeFormat)})
table.Render()
fmt.Println("代码:")
fmt.Println(code)
},
}
var solutionDiffCmd = &cobra.Command{
Use: "diff question_id|leetcode_url solution_no_1 solution_no_2",
Short: "比较两次代码的不同之处",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 3 {
cmd.Println("参数异常")
cmd.Help()
os.Exit(1)
return
}
titleSlug := leet.Parse(strings.TrimSpace(args[0]))
solutionIndex := strings.TrimSpace(args[1])
idx, err := strconv.Atoi(solutionIndex)
if err != nil {
fmt.Println(err.Error())
return
}
idx--
solutionIndex2 := strings.TrimSpace(args[2])
idx2, err := strconv.Atoi(solutionIndex2)
if err != nil {
fmt.Println(err.Error())
return
}
idx2--
info := leet.GetQuestionInfo(titleSlug)
solutions, err := store.GetSolution(info.QuestionID)
if err != nil {
fmt.Println(err.Error())
return
}
if idx < 0 || idx >= len(solutions) || idx2 < 0 || idx2 >= len(solutions) {
idx++
fmt.Println(fmt.Sprintf("最大题解数为: %d, 当前输入: %d %d", len(solutions), idx, idx2))
return
}
solution1 := solutions[idx]
solution2 := solutions[idx2]
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(
leet.CustomerCode(solution1.Code),
leet.CustomerCode(solution2.Code),
true)
for _, diff := range diffs {
switch diff.Type {
case diffmatchpatch.DiffDelete:
fmt.Print("---")
case diffmatchpatch.DiffInsert:
fmt.Print("+++")
}
fmt.Print(diff.Text)
}
},
}
var solutionTidyCmd = &cobra.Command{
Use: "tidy question_id|leetcode_url solution_no",
Short: "清空单个问题所有题解",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Println("参数异常")
cmd.Help()
os.Exit(1)
return
}
titleSlug := leet.Parse(strings.TrimSpace(args[0]))
info := leet.GetQuestionInfo(titleSlug)
err := store.TidySolution(info.QuestionID)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("整理完成")
},
}
var solutionEmptyCmd = &cobra.Command{
Use: "empty question_id|leetcode_url solution_no",
Short: "清空单个问题所有题解",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Println("参数异常")
cmd.Help()
os.Exit(1)
return
}
titleSlug := leet.Parse(strings.TrimSpace(args[0]))
info := leet.GetQuestionInfo(titleSlug)
err := store.RemoveSolution(info.QuestionID)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("清空完成")
},
}