Skip to content

Commit e365e59

Browse files
author
Shuo
authored
Merge pull request #633 from openset/develop
Add: v1.4.5
2 parents 18f2a26 + 838c5ef commit e365e59

File tree

11 files changed

+64
-58
lines changed

11 files changed

+64
-58
lines changed

Diff for: .travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: go
22

33
go:
4-
- "1.12.x"
4+
- "1.13.x"
55

66
env:
77
- GO111MODULE=on

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/openset/leetcode
22

3-
go 1.12
3+
go 1.13

Diff for: internal/base/base.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io/ioutil"
88
"log"
99
"os"
10-
"path"
1110
"path/filepath"
1211
"strings"
1312
"sync"
@@ -86,7 +85,7 @@ func JsonIndent(src []byte) []byte {
8685
}
8786

8887
func getFilePath(filename string) string {
89-
if dir := path.Dir(filename); dir != "" {
88+
if dir := filepath.Dir(filename); dir != "" {
9089
if err := os.MkdirAll(dir, 0755); err != nil {
9190
CheckErr(err)
9291
}

Diff for: internal/description/description.go

+20-13
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,29 @@ func runDescription(cmd *base.Command, args []string) {
2020
cmd.Usage()
2121
}
2222
var wg sync.WaitGroup
23-
tokens := make(chan bool, 1<<7)
23+
limit := 1 << 7
24+
jobs := make(chan *leetcode.StatStatusPairsType, limit)
25+
for i := 0; i < limit; i++ {
26+
go worker(jobs, &wg)
27+
}
2428
problems := leetcode.ProblemsAll()
2529
for _, problem := range problems.StatStatusPairs {
26-
wg.Add(1)
27-
tokens <- true
30+
problem := problem
2831
fmt.Println(problem.Stat.FrontendQuestionId, "\t"+problem.Stat.QuestionTitle)
29-
go func(problem leetcode.StatStatusPairsType) {
30-
titleSlug := problem.Stat.QuestionTitleSlug
31-
question := leetcode.QuestionData(titleSlug, false).Data.Question
32-
if question.Content == "" && problem.PaidOnly == true && problem.Stat.QuestionArticleLive {
33-
question.Content = leetcode.GetDescription(problem.Stat.QuestionArticleSlug)
34-
}
35-
question.SaveContent()
36-
<-tokens
37-
wg.Done()
38-
}(problem)
32+
wg.Add(1)
33+
jobs <- &problem
3934
}
4035
wg.Wait()
4136
}
37+
38+
func worker(jobs <-chan *leetcode.StatStatusPairsType, wg *sync.WaitGroup) {
39+
for problem := range jobs {
40+
titleSlug := problem.Stat.QuestionTitleSlug
41+
question := leetcode.QuestionData(titleSlug, false).Data.Question
42+
if question.Content == "" && problem.PaidOnly == true && problem.Stat.QuestionArticleLive {
43+
question.Content = leetcode.GetDescription(problem.Stat.QuestionArticleSlug)
44+
}
45+
question.SaveContent()
46+
wg.Done()
47+
}
48+
}

Diff for: internal/leetcode/base.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http"
77
"net/url"
88
"os"
9-
"path"
9+
"path/filepath"
1010
"strings"
1111
"time"
1212

@@ -60,7 +60,7 @@ func getCachePath(f string) string {
6060
dir, err = os.UserCacheDir()
6161
checkErr(err)
6262
}
63-
return path.Join(dir, ".leetcode", f)
63+
return filepath.Join(dir, ".leetcode", f)
6464
}
6565

6666
func Clean() {

Diff for: internal/leetcode/problems_all.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type difficultyType struct {
6262

6363
type paidType bool
6464

65-
func (problem StatStatusPairsType) WriteRow(buf *bytes.Buffer) {
65+
func (problem *StatStatusPairsType) WriteRow(buf *bytes.Buffer) {
6666
format := "| <span id=\"%d\">%d</span> | [%s](https://leetcode.com/problems/%s%s)%s | [%s](https://github.com/openset/leetcode/tree/master/problems/%s) | %s |\n"
6767
id := problem.Stat.FrontendQuestionId
6868
stat := problem.Stat
@@ -79,23 +79,23 @@ func (p paidType) Str() string {
7979
return ""
8080
}
8181

82-
func (s statType) QuestionTitleSnake() string {
82+
func (s *statType) QuestionTitleSnake() string {
8383
return slugToSnake(s.QuestionTitleSlug)
8484
}
8585

86-
func (s statType) TranslationTitle() string {
86+
func (s *statType) TranslationTitle() string {
8787
title := translationSet[s.QuestionId]
8888
if title != "" {
8989
title = fmt.Sprintf(` "%s"`, title)
9090
}
9191
return title
9292
}
9393

94-
func (s statType) Lang() string {
94+
func (s *statType) Lang() string {
9595
return getLang(s.QuestionTitleSlug)
9696
}
9797

98-
func (d difficultyType) LevelName() string {
98+
func (d *difficultyType) LevelName() string {
9999
m := map[int]string{
100100
1: "Easy",
101101
2: "Medium",

Diff for: internal/leetcode/question_data.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"log"
77
"os"
8-
"path"
8+
"path/filepath"
99
"regexp"
1010
"strconv"
1111
"strings"
@@ -102,14 +102,14 @@ func (d difficultyStrType) Str() (s string) {
102102
return
103103
}
104104

105-
func (question questionType) SaveContent() {
105+
func (question *questionType) SaveContent() {
106106
if question.TitleSlug != "" {
107107
filePutContents(question.getFilePath("README.md"), question.getDescContent())
108108
question.saveMysqlSchemas()
109109
}
110110
}
111111

112-
func (question questionType) getDescContent() []byte {
112+
func (question *questionType) getDescContent() []byte {
113113
var buf bytes.Buffer
114114
buf.WriteString(authInfo("description"))
115115
buf.WriteString(question.getNavigation())
@@ -127,7 +127,7 @@ func (question questionType) getDescContent() []byte {
127127
return buf.Bytes()
128128
}
129129

130-
func (question questionType) getNavigation() string {
130+
func (question *questionType) getNavigation() string {
131131
nav, pre, next := "\n%s\n%s\n%s\n", "< Previous", "Next >"
132132
problems := ProblemsAll().StatStatusPairs
133133
if questionId, err := strconv.Atoi(question.QuestionId); err == nil {
@@ -147,7 +147,7 @@ func (question questionType) getNavigation() string {
147147
return fmt.Sprintf(nav, pre, strings.Repeat(" ", 16), next)
148148
}
149149

150-
func (question questionType) getTopicTags() []byte {
150+
func (question *questionType) getTopicTags() []byte {
151151
tags := question.TopicTags
152152
var buf bytes.Buffer
153153
if len(tags) > 0 {
@@ -160,12 +160,12 @@ func (question questionType) getTopicTags() []byte {
160160
return buf.Bytes()
161161
}
162162

163-
func (question questionType) GetSimilarQuestion() (sq []similarQuestionType) {
163+
func (question *questionType) GetSimilarQuestion() (sq []similarQuestionType) {
164164
jsonDecode([]byte(question.SimilarQuestions), &sq)
165165
return
166166
}
167167

168-
func (question questionType) getSimilarQuestion() []byte {
168+
func (question *questionType) getSimilarQuestion() []byte {
169169
sq := question.GetSimilarQuestion()
170170
var buf bytes.Buffer
171171
if len(sq) > 0 {
@@ -178,7 +178,7 @@ func (question questionType) getSimilarQuestion() []byte {
178178
return buf.Bytes()
179179
}
180180

181-
func (question questionType) getHints() []byte {
181+
func (question *questionType) getHints() []byte {
182182
hints := question.Hints
183183
var buf bytes.Buffer
184184
if len(hints) > 0 {
@@ -190,29 +190,29 @@ func (question questionType) getHints() []byte {
190190
return buf.Bytes()
191191
}
192192

193-
func (question questionType) getFilePath(filename string) string {
194-
return path.Join("problems", question.TitleSlug, filename)
193+
func (question *questionType) getFilePath(filename string) string {
194+
return filepath.Join("problems", question.TitleSlug, filename)
195195
}
196196

197-
func (question questionType) TitleSnake() string {
197+
func (question *questionType) TitleSnake() string {
198198
return slugToSnake(question.TitleSlug)
199199
}
200200

201-
func (question questionType) PackageName() string {
201+
func (question *questionType) PackageName() string {
202202
snake := question.TitleSnake()
203203
if snake != "" && unicode.IsNumber(rune(snake[0])) {
204204
snake = "p_" + snake
205205
}
206206
return snake
207207
}
208208

209-
func (question questionType) SaveCodeSnippet() {
209+
func (question *questionType) SaveCodeSnippet() {
210210
if isLangMySQL(question.TitleSlug) {
211211
filePutContents(question.getFilePath(question.TitleSnake()+".sql"), []byte("# Write your MySQL query statement below\n"))
212212
}
213213
langSupport := [...]struct {
214214
slug string
215-
handle func(questionType, codeSnippetsType)
215+
handle func(*questionType, codeSnippetsType)
216216
}{
217217
{"golang", handleCodeGolang},
218218
{"python3", handleCodePython},
@@ -234,23 +234,23 @@ func (question questionType) SaveCodeSnippet() {
234234
}
235235
}
236236

237-
func (question questionType) saveCodeContent(content, ext string, permX ...bool) {
237+
func (question *questionType) saveCodeContent(content, ext string, permX ...bool) {
238238
filePath := question.getFilePath(question.TitleSnake() + ext)
239239
filePutContents(filePath, []byte(content))
240240
if len(permX) > 0 && permX[0] == true {
241241
_ = os.Chmod(filePath, 0755)
242242
}
243243
}
244244

245-
func (question questionType) saveMysqlSchemas() {
245+
func (question *questionType) saveMysqlSchemas() {
246246
var buf bytes.Buffer
247247
for _, s := range question.MysqlSchemas {
248248
buf.WriteString(s + ";\n")
249249
}
250250
filePutContents(question.getFilePath("mysql_schemas.sql"), buf.Bytes())
251251
}
252252

253-
func handleCodeGolang(question questionType, code codeSnippetsType) {
253+
func handleCodeGolang(question *questionType, code codeSnippetsType) {
254254
content := fmt.Sprintf("package %s\n\n", question.PackageName())
255255
content += code.Code + "\n"
256256
question.saveCodeContent(content, ".go")
@@ -272,15 +272,15 @@ func handleCodeGolang(question questionType, code codeSnippetsType) {
272272
}
273273
}
274274

275-
func handleCodeBash(question questionType, code codeSnippetsType) {
275+
func handleCodeBash(question *questionType, code codeSnippetsType) {
276276
question.saveCodeContent("#!/usr/bin/env bash\n\n"+code.Code, ".bash", true)
277277
}
278278

279-
func handleCodePython(question questionType, code codeSnippetsType) {
279+
func handleCodePython(question *questionType, code codeSnippetsType) {
280280
question.saveCodeContent("#!/usr/bin/env python\n\n"+code.Code, ".py", true)
281281
}
282282

283-
func handleCodeSQL(question questionType, code codeSnippetsType) {
283+
func handleCodeSQL(question *questionType, code codeSnippetsType) {
284284
question.saveCodeContent(code.Code, ".sql")
285285
question.saveMysqlSchemas()
286286
}

Diff for: internal/leetcode/topic_tag.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"log"
77
"os"
8-
"path"
8+
"path/filepath"
99
"regexp"
1010
"sort"
1111
"strconv"
@@ -16,7 +16,7 @@ import (
1616

1717
var (
1818
initTags []TagType
19-
tagsFile = path.Join("tag", "tags.json")
19+
tagsFile = filepath.Join("tag", "tags.json")
2020
)
2121

2222
func init() {
@@ -120,7 +120,7 @@ type ttQuestionType struct {
120120
TopicTags []TagType `json:"topicTags"`
121121
}
122122

123-
func (question ttQuestionType) TagsStr() string {
123+
func (question *ttQuestionType) TagsStr() string {
124124
var buf bytes.Buffer
125125
format := "[[%s](https://github.com/openset/leetcode/tree/master/tag/%s/README.md)] "
126126
for _, tag := range question.TopicTags {
@@ -130,7 +130,7 @@ func (question ttQuestionType) TagsStr() string {
130130
return string(buf.Bytes())
131131
}
132132

133-
func (tag TagType) SaveContents() {
133+
func (tag *TagType) SaveContents() {
134134
questions := GetTopicTag(tag.Slug).Data.TopicTag.Questions
135135
sort.Slice(questions, func(i, j int) bool {
136136
m, _ := strconv.Atoi(questions[i].QuestionFrontendId)
@@ -149,11 +149,11 @@ func (tag TagType) SaveContents() {
149149
}
150150
buf.WriteString(fmt.Sprintf(format, question.QuestionFrontendId, question.TranslatedTitle, question.TitleSlug, question.IsPaidOnly.Str(), question.TagsStr(), question.Difficulty))
151151
}
152-
filename := path.Join("tag", tag.Slug, "README.md")
152+
filename := filepath.Join("tag", tag.Slug, "README.md")
153153
filePutContents(filename, buf.Bytes())
154154
}
155155

156-
func (tag TagType) ShowName() string {
156+
func (tag *TagType) ShowName() string {
157157
if tag.TranslatedName != "" {
158158
return tag.TranslatedName
159159
}

Diff for: internal/post/post.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"os"
7-
"path"
7+
"path/filepath"
88
"regexp"
99
"strings"
1010
"time"
@@ -80,8 +80,8 @@ func runPost(cmd *base.Command, args []string) {
8080
buf.WriteString("\n---\n")
8181
buf.WriteString(fmt.Sprintf("\n## [答案](https://github.com/openset/leetcode/tree/master/problems/%s)\n", question.TitleSlug))
8282
filename := fmt.Sprintf(formatFilename, t.Format("2006-01-02"), question.TitleSlug)
83-
oldPath := path.Join(basePath, "leetcode", filename)
84-
newPath := path.Join(basePath, "_posts", filename)
83+
oldPath := filepath.Join(basePath, "leetcode", filename)
84+
newPath := filepath.Join(basePath, "_posts", filename)
8585
base.FilePutContents(oldPath, buf.Bytes())
8686
if inPosts[questionId] {
8787
_ = os.Rename(oldPath, newPath)
@@ -101,7 +101,7 @@ func postTags() {
101101
}
102102
filename := fmt.Sprintf("tag-%s.md", tag.Slug)
103103
data := []byte(fmt.Sprintf(tagTmpl, title, tag.Slug, tag.Name))
104-
base.FilePutContents(path.Join(basePath, "_pages", filename), data)
104+
base.FilePutContents(filepath.Join(basePath, "_pages", filename), data)
105105
}
106106
}
107107

@@ -125,7 +125,7 @@ taxonomy: %s
125125

126126
var homeDir, _ = os.UserHomeDir()
127127

128-
var basePath = path.Join(homeDir, "openset", "openset")
128+
var basePath = filepath.Join(homeDir, "openset", "openset")
129129

130130
var inPosts = map[int]bool{
131131
1: true,

Diff for: internal/readme/readme.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package readme
33
import (
44
"bytes"
55
"fmt"
6-
"path"
6+
"path/filepath"
77

88
"github.com/openset/leetcode/internal/base"
99
"github.com/openset/leetcode/internal/leetcode"
@@ -47,7 +47,7 @@ func writeProblems(buf *bytes.Buffer) {
4747
count--
4848
problems[count].WriteRow(buf)
4949
}
50-
fileName := path.Join("readme", fmt.Sprintf("%d-%d.md", pageSize*(i-1)+1, pageSize*i))
50+
fileName := filepath.Join("readme", fmt.Sprintf("%d-%d.md", pageSize*(i-1)+1, pageSize*i))
5151
base.FilePutContents(fileName, buf.Bytes())
5252
buf.Truncate(n)
5353
}

Diff for: internal/version/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/openset/leetcode/internal/base"
88
)
99

10-
const version = "1.4.4"
10+
const version = "1.4.5"
1111

1212
var CmdVersion = &base.Command{
1313
Run: runVersion,

0 commit comments

Comments
 (0)