Skip to content

Commit

Permalink
feat: support Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
zcong1993 committed Dec 2, 2020
1 parent 777b06c commit aed4de0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 33 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ $ brew install zcong1993/homebrew-tap/leetcode-tool
$ leetcode-tool help
```

## 使用说明

[https://blog.cong.moe/post/2020-11-30-leetcode_tool](https://blog.cong.moe/post/2020-11-30-leetcode_tool)

## 支持语言

- Golang go
- Typescript ts
- Javascript js
- Python3 py3

## 主要功能

### 新建题目代码
Expand Down
82 changes: 49 additions & 33 deletions cmd/new/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ import (
"github.com/zcong1993/leetcode-tool/pkg/leetcode"
)

type TplFile struct {
Name string
FileName string
TplStr string
}

type LanguageConfig struct {
LeetcodeLang string
CodeTplStr string
TestCodeTplStr string
CodeFileName string
TestFileName string
LeetcodeLang string
TplFiles []TplFile
}

const (
Expand All @@ -33,25 +36,20 @@ const (
var (
languageConfigs = map[string]LanguageConfig{
"go": {
CodeTplStr: codeStrGo,
TestCodeTplStr: testCodeStrGo,
LeetcodeLang: "Go",
CodeFileName: "solve_%s.go",
TestFileName: "solve_%s_test.go",
LeetcodeLang: "Go",
TplFiles: []TplFile{{"code", "solve_%s.go", codeStrGo}, {"test", "solve_%s_test.go", testCodeStrGo}},
},
"ts": {
CodeTplStr: codeStrTs,
TestCodeTplStr: testCodeStrTs,
LeetcodeLang: "TypeScript",
CodeFileName: "solve_%s.ts",
TestFileName: "solve_%s.test.ts",
LeetcodeLang: "TypeScript",
TplFiles: []TplFile{{"code", "solve_%s.ts", codeStrTs}, {"test", "solve_%s.test.ts", testCodeStrTs}},
},
"js": {
CodeTplStr: codeStrJs,
TestCodeTplStr: testCodeStrJs,
LeetcodeLang: "JavaScript",
CodeFileName: "solve_%s.js",
TestFileName: "solve_%s.test.js",
LeetcodeLang: "JavaScript",
TplFiles: []TplFile{{"code", "solve_%s.js", codeStrJs}, {"test", "solve_%s.test.js", testCodeStrJs}},
},
"py3": {
LeetcodeLang: "Python3",
TplFiles: []TplFile{{"code", "solve_%s.py", codeStrPy3}, {"test", "test_%s.py", testCodeStrPy3}, {"__init__", "__init__.py", ""}},
},
}
)
Expand Down Expand Up @@ -107,9 +105,6 @@ func Run(n string, lang string) {
folderName := prefix + number
fp := filepath.Join(folder, folderName)
os.MkdirAll(fp, 0755)
codeFp := filepath.Join(fp, fmt.Sprintf(config.CodeFileName, number))
codeTestFp := filepath.Join(fp, fmt.Sprintf(config.TestFileName, number))
problemFp := filepath.Join(fp, "problem.md")
metaf := &MetaWithFolder{
*meta,
folderName,
Expand All @@ -119,20 +114,23 @@ func Run(n string, lang string) {
metaf.Meta.Content = strings.ReplaceAll(metaf.Meta.Content, "↵", "")
metaf.Meta.Code = gjson.Get(metaf.CodeSnippets, fmt.Sprintf("#(lang=%s).code", config.LeetcodeLang)).String()

if !fileExists(codeFp) {
bf := mustExecuteTemplate("code", config.CodeTplStr, metaf)
ioutil.WriteFile(codeFp, bf, 0644)
}

if !fileExists(codeTestFp) {
bf := mustExecuteTemplate("test", config.TestCodeTplStr, metaf)
ioutil.WriteFile(codeTestFp, bf, 0644)
}

problemFp := filepath.Join(fp, "problem.md")
if !fileExists(problemFp) {
bf := mustExecuteTemplate("problem", problemStr, metaf)
ioutil.WriteFile(problemFp, bf, 0644)
}

for _, tpl := range config.TplFiles {
fileName := tpl.FileName
if strings.Count(tpl.FileName, "%s") > 0 {
fileName = fmt.Sprintf(tpl.FileName, number)
}
fp := filepath.Join(fp, fileName)
if !fileExists(fp) {
bf := mustExecuteTemplate(tpl.Name, tpl.TplStr, metaf)
ioutil.WriteFile(fp, bf, 0644)
}
}
fmt.Printf("Done: %s\n", fp)
}

Expand Down Expand Up @@ -197,3 +195,21 @@ var (
it('solve_{{ .Index }} should pass', () => {})
`
)

var (
codeStrPy3 = `'''
@index {{ .Index }}
@title {{ .Title }}
@difficulty {{ .Difficulty }}
@tags {{ .TagStr }}
@draft false
@link {{ .Link }}
@frontendId {{ .FrontendId }}
'''
{{ .Code }}
`
testCodeStrPy3 = `def test_solve():
pass
`
)

0 comments on commit aed4de0

Please sign in to comment.