Skip to content

Commit aed4de0

Browse files
committed
feat: support Python3
1 parent 777b06c commit aed4de0

File tree

2 files changed

+60
-33
lines changed

2 files changed

+60
-33
lines changed

Diff for: README.md

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ $ brew install zcong1993/homebrew-tap/leetcode-tool
1616
$ leetcode-tool help
1717
```
1818

19+
## 使用说明
20+
21+
[https://blog.cong.moe/post/2020-11-30-leetcode_tool](https://blog.cong.moe/post/2020-11-30-leetcode_tool)
22+
23+
## 支持语言
24+
25+
- Golang go
26+
- Typescript ts
27+
- Javascript js
28+
- Python3 py3
29+
1930
## 主要功能
2031

2132
### 新建题目代码

Diff for: cmd/new/main.go

+49-33
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ import (
1717
"github.com./zcong1993/leetcode-tool/pkg/leetcode"
1818
)
1919

20+
type TplFile struct {
21+
Name string
22+
FileName string
23+
TplStr string
24+
}
25+
2026
type LanguageConfig struct {
21-
LeetcodeLang string
22-
CodeTplStr string
23-
TestCodeTplStr string
24-
CodeFileName string
25-
TestFileName string
27+
LeetcodeLang string
28+
TplFiles []TplFile
2629
}
2730

2831
const (
@@ -33,25 +36,20 @@ const (
3336
var (
3437
languageConfigs = map[string]LanguageConfig{
3538
"go": {
36-
CodeTplStr: codeStrGo,
37-
TestCodeTplStr: testCodeStrGo,
38-
LeetcodeLang: "Go",
39-
CodeFileName: "solve_%s.go",
40-
TestFileName: "solve_%s_test.go",
39+
LeetcodeLang: "Go",
40+
TplFiles: []TplFile{{"code", "solve_%s.go", codeStrGo}, {"test", "solve_%s_test.go", testCodeStrGo}},
4141
},
4242
"ts": {
43-
CodeTplStr: codeStrTs,
44-
TestCodeTplStr: testCodeStrTs,
45-
LeetcodeLang: "TypeScript",
46-
CodeFileName: "solve_%s.ts",
47-
TestFileName: "solve_%s.test.ts",
43+
LeetcodeLang: "TypeScript",
44+
TplFiles: []TplFile{{"code", "solve_%s.ts", codeStrTs}, {"test", "solve_%s.test.ts", testCodeStrTs}},
4845
},
4946
"js": {
50-
CodeTplStr: codeStrJs,
51-
TestCodeTplStr: testCodeStrJs,
52-
LeetcodeLang: "JavaScript",
53-
CodeFileName: "solve_%s.js",
54-
TestFileName: "solve_%s.test.js",
47+
LeetcodeLang: "JavaScript",
48+
TplFiles: []TplFile{{"code", "solve_%s.js", codeStrJs}, {"test", "solve_%s.test.js", testCodeStrJs}},
49+
},
50+
"py3": {
51+
LeetcodeLang: "Python3",
52+
TplFiles: []TplFile{{"code", "solve_%s.py", codeStrPy3}, {"test", "test_%s.py", testCodeStrPy3}, {"__init__", "__init__.py", ""}},
5553
},
5654
}
5755
)
@@ -107,9 +105,6 @@ func Run(n string, lang string) {
107105
folderName := prefix + number
108106
fp := filepath.Join(folder, folderName)
109107
os.MkdirAll(fp, 0755)
110-
codeFp := filepath.Join(fp, fmt.Sprintf(config.CodeFileName, number))
111-
codeTestFp := filepath.Join(fp, fmt.Sprintf(config.TestFileName, number))
112-
problemFp := filepath.Join(fp, "problem.md")
113108
metaf := &MetaWithFolder{
114109
*meta,
115110
folderName,
@@ -119,20 +114,23 @@ func Run(n string, lang string) {
119114
metaf.Meta.Content = strings.ReplaceAll(metaf.Meta.Content, "↵", "")
120115
metaf.Meta.Code = gjson.Get(metaf.CodeSnippets, fmt.Sprintf("#(lang=%s).code", config.LeetcodeLang)).String()
121116

122-
if !fileExists(codeFp) {
123-
bf := mustExecuteTemplate("code", config.CodeTplStr, metaf)
124-
ioutil.WriteFile(codeFp, bf, 0644)
125-
}
126-
127-
if !fileExists(codeTestFp) {
128-
bf := mustExecuteTemplate("test", config.TestCodeTplStr, metaf)
129-
ioutil.WriteFile(codeTestFp, bf, 0644)
130-
}
131-
117+
problemFp := filepath.Join(fp, "problem.md")
132118
if !fileExists(problemFp) {
133119
bf := mustExecuteTemplate("problem", problemStr, metaf)
134120
ioutil.WriteFile(problemFp, bf, 0644)
135121
}
122+
123+
for _, tpl := range config.TplFiles {
124+
fileName := tpl.FileName
125+
if strings.Count(tpl.FileName, "%s") > 0 {
126+
fileName = fmt.Sprintf(tpl.FileName, number)
127+
}
128+
fp := filepath.Join(fp, fileName)
129+
if !fileExists(fp) {
130+
bf := mustExecuteTemplate(tpl.Name, tpl.TplStr, metaf)
131+
ioutil.WriteFile(fp, bf, 0644)
132+
}
133+
}
136134
fmt.Printf("Done: %s\n", fp)
137135
}
138136

@@ -197,3 +195,21 @@ var (
197195
it('solve_{{ .Index }} should pass', () => {})
198196
`
199197
)
198+
199+
var (
200+
codeStrPy3 = `'''
201+
@index {{ .Index }}
202+
@title {{ .Title }}
203+
@difficulty {{ .Difficulty }}
204+
@tags {{ .TagStr }}
205+
@draft false
206+
@link {{ .Link }}
207+
@frontendId {{ .FrontendId }}
208+
'''
209+
210+
{{ .Code }}
211+
`
212+
testCodeStrPy3 = `def test_solve():
213+
pass
214+
`
215+
)

0 commit comments

Comments
 (0)