8000 update cmd library · liaol/awesome-golang-leetcode@b7a397f · GitHub
[go: up one dir, main page]

Skip to content

Commit b7a397f

Browse files
committed
update cmd library
1 parent b8df881 commit b7a397f

File tree

12 files changed

+175
-134
lines changed

12 files changed

+175
-134
lines changed

cmd/leetcode/makedir.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"os"
88
)
99

10-
const SOLUTIONS_APTH = "solutions/"
10+
const SOLUTIONS_PATH = "src/"
1111
const SOURCE_SOLUTION_FILE_PATH = "cmd/template/solution/solution.go"
1212
const SOURCE_SOLUTION_TEST_FILE_PATH = "cmd/template/solution/solution_test.go"
1313
const SOURCE_SOLUTION_README_FILE_PATH = "cmd/template/solution/README.md"
@@ -28,17 +28,18 @@ func MakeDir(problems []Problem) {
2828
continue
2929
}
3030

31-
if is_DirExists, _ := PathExists(SOLUTIONS_APTH + problems[i].PathName); is_DirExists {
32-
log.Println("目录已经存在:", SOLUTIONS_APTH+problems[i].PathName)
31+
if is_DirExists, _ := PathExists(SOLUTIONS_PATH + problems[i].PathName); is_DirExists {
32+
log.Println("目录已经存在:", SOLUTIONS_PATH+problems[i].PathName)
3333
} else {
34-
err := os.Mkdir(SOLUTIONS_APTH+problems[i].PathName, os.ModePerm)
34+
err := os.Mkdir(SOLUTIONS_PATH+problems[i].PathName, os.ModePerm)
3535
if err != nil {
3636
log.Printf("目录创建失败:%s", err.Error())
3737
} else {
3838
// 复制文件
3939
log.Println("开始复制文件:")
40-
copy(SOURCE_SOLUTION_FILE_PATH, SOLUTIONS_APTH+problems[i].PathName+"/solution.go")
41-
copy(SOURCE_SOLUTION_TEST_FILE_PATH, SOLUTIONS_APTH+problems[i].PathName+"/solution_test.go")
40+
copy(SOURCE_SOLUTION_FILE_PATH, SOLUTIONS_PATH+problems[i].PathName+"/Solution.go")
41+
copy(SOURCE_SOLUTION_TEST_FILE_PATH, SOLUTIONS_PATH+problems[i].PathName+"/Solution_test.go")
42+
GenerateReadme(problems[i])
4243
}
4344
}
4445

cmd/leetcode/makedir_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package leetcode
22

33
import (
4+
"fmt"
45
"testing"
56
)
67

78
func TestMakeDir(t *testing.T) {
8-
//problems := GetProblemsInstance()
9+
problems := GetSotedproblemsInstance()
10+
fmt.Println(len(problems))
11+
12+
for _, v := range problems {
13+
if v.PaidOnly {
14+
fmt.Println(v)
15+
}
16+
fmt.Println(v)
17+
}
918

10-
//for _, v := range problems {
11-
// if v.PaidOnly {
12-
//
13-
// fmt.Println(v)
14-
// }
15-
//}
1619
//MakeDir(problems)
1720
}

cmd/leetcode/problem-readme.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
package leetcode
22

33
import (
4+
"bytes"
45
"fmt"
6+
"html/template"
57
"io/ioutil"
68
"log"
9+
"os"
710
)
811

912
func GetReadmeTemplateBuffer() string {
10-
data, err := ioutil.ReadFile(SOURCE_SOLUTION_README_FILE_PATH)
13+
data, err := ioutil.ReadFile(SOURCE_SOLUTION_SUMMARY_FILE_PATH)
1114
if err != nil {
1215
log.Panicln("读取 README模板失败", err.Error())
1316
}
1417
fmt.Println(string(data))
1518
return string(data)
1619
}
20+
21+
func GenerateReadme(problem Problem) {
22+
log.Println("开始生成 README")
23+
file, err := os.OpenFile(SOURCE_SOLUTION_README_FILE_PATH, os.O_RDONLY, 0600)
24+
defer file.Close()
25+
if err != nil {
26+
log.Panicf("README 模板读取失败1:%s", err.Error())
27+
}
28+
29+
buffer, err := ioutil.ReadAll(file)
30+
if err != nil {
31+
log.Panicf("README 模板读取失败2:%s", err.Error())
32+
}
33+
34+
var tmpRes bytes.Buffer
35+
36+
tmpl, err := template.New("README: ").Parse(string(buffer))
37+
err = tmpl.Execute(&tmpRes, problem)
38+
write(SOLUTIONS_PATH+problem.PathName+"/README.md", string(tmpRes.Bytes()))
39+
}

cmd/leetcode/problem.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package leetcode
22

33
import (
44
"encoding/json"
5+
"errors"
6+
"fmt"
57
"io/ioutil"
68
"log"
79
"net/http"
@@ -32,6 +34,7 @@ type Problem struct {
3234
Frequency int `json:"frequency"`
3335
Progress int `json:"progress"`
3436
PathName string `json:"path_name"`
37+
DirPath string `json:"dir_path"`
3538
}
3639
type Stat struct {
3740
QuestionID int `json:"question_id"`
@@ -80,7 +83,7 @@ func GetProblemsInstance() []Problem {
8083
return leetcode.StatStatusPairs
8184
}
8285

83-
func GetSotedproblemsInstance() []Problem {
86+
func GetSortedProblemsInstance() []Problem {
8487
problems := GetProblemsInstance()
8588
// 插入排序
8689
problems = sortProblems(problems)
@@ -102,7 +105,7 @@ func sortProblems(problems []Problem) []Problem {
102105
return problems
103106
}
104107

105-
func GetProblemsJosn() string {
108+
func GetProblemsJson() string {
106109
problems := GetProblemsInstance()
107110
problem_string, err := json.MarshalIndent(problems, " ", " ")
108111
if err != nil {
@@ -149,11 +152,23 @@ func formatId(id int) string {
149152
func formatName(name string) string {
150153
str := ""
151154
for i, v := range name {
155+
if v == ' ' && name[i-1] != '-' {
156+
str = str + "-"
157+
continue
158+
}
152159
if v == ' ' {
160+
continue
161+
}
162+
if v == '`' {
153163
str = str + "-"
154164
continue
155165
}
156-
if v == '(' {
166+
if v == '%' {
167+
str = str + "p"
168+
continue
169+
}
170+
171+
if v == '(' && name[i-1] != '-' {
157172
str = str + "-"
158173
continue
159174
}
@@ -164,6 +179,13 @@ func formatName(name string) string {
164179
if v == ',' {
165180
continue
166181
}
182+
// ' 过滤
183+
if v == 39 {
184+
continue
185+
}
186+
if v == '?' {
187+
continue
188+
}
167189
if i > 0 && name[i-1] == '-' {
168190
str = str + string(v)
169191
continue
@@ -175,3 +197,18 @@ func formatName(name string) string {
175197
}
176198
return str
177199
}
200+
201+
func UrlPath(path string) error {
202+
resp, err := http.Get(path)
203+
if err != nil {
204+
fmt.Println(err)
205+
return errors.New(path + " " + err.Error())
206+
}
207+
208+
if resp.StatusCode != 200 {
209+
fmt.Println(resp.StatusCode)
210+
return errors.New(string(resp.StatusCode))
211+
}
212+
213+
return nil
214+
}

cmd/leetcode/problem_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestUrlPath(t *testing.T) {
8+
t.Run("1", func(t *testing.T) {
9+
err := UrlPath("https://www.baidu.com")
10+
if err != nil {
11+
t.Fatal(err.Error())
12+
}
13+
})
14+
}

cmd/main.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
package main
22

3-
func main() {
4-
//problems := leetcode.GetSotedproblemsInstance()
5-
//problems := leetcode.GetProblemsJosn()
3+
import (
4+
"github.com/kylesliu/awesome-golang-leetcode/cmd/leetcode"
5+
)
66

7+
func main() {
8+
problems := leetcode.GetSortedProblemsInstance()
9+
//problems := leetcode.GetProblemsJson()
10+
//fmt.Println(problems)
711
//for _, v := range problems {
8-
// fmt.Println(v.Stat.QuestionID)
9-
// fmt.Println(v.Stat.QuestionTitle)
10-
// fmt.Println(v.PathName)
12+
// fmt.Println(v.Stat.QuestionID, v.Stat.QuestionTitle, v.PathName,v.Stat.QuestionTitleSlup)
13+
//err := leetcode.UrlPath("https://leetcode.com/problems/" + v.Stat.QuestionTitleSlup )
14+
//if err != nil {
15+
// fmt.Println(v.Stat.QuestionID, v.Stat.QuestionTitle, v.PathName,v.Stat.QuestionTitleSlup)
16+
// fmt.Println(err.Error())
17+
//}
1118
//}
1219

1320
// 生成Problem 目录
21+
1422
//leetcode.MakeDir(problems)
1523

1624
//leetcode.GetReadmeTemplateBuffer()
1725

1826
// GitBook
19-
//leetcode.MakeGitbookSummary(problems)
27+
leetcode.MakeGitbookSummary(problems)
2028

2129
}

cmd/template/gitbook/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
* [Contributor](CONTRIBUTOR.md)
55
* Soutions(1-976)
66
{{ range . }}* [{{.PathName}}](./src/{{.PathName}}/README.md)
7-
{{ end }}
7+
{{ end }}

cmd/template/solution/README.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
1-
# Summary
2-
3-
* [Summary](SUMMARY-LIST.md)
4-
* Soutions(1-976)
5-
{{ range . }}* [{{.PathName}}](./src/{{.PathName}}/README.md)
6-
{{ end }}
1+
# [{{.Stat.FrontendQuestionID}}.{{ .Stat.QuestionTitle}}][title]
2+
3+
> [!WARNING|style:flat]
4+
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-leetcode)
5+
6+
## Description
7+
8+
**Example 1:**
9+
10+
```
11+
Input: a = "11", b = "1"
12+
Output: "100"
13+
```
14+
15+
## 题意
16+
> ...
17+
18+
## 题解
19+
20+
### 思路1
21+
> ...
22+
{{ .Stat.QuestionTitle}}
23+
```go
24+
```
25+
26+
27+
## 结语
28+
29+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
30+
31+
[title]: https://leetcode.com/problems/{{ .Stat.QuestionTitleSlup }}/
32+
[me]: https://github.com/kylesliu/awesome-golang-leetcode

cmd/template/solution/solution.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

cmd/template/solution/solution_test.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

cmd/tree/tree.go

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0