Skip to content

Add: v1.4.5 #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- "1.12.x"
- "1.13.x"

env:
- GO111MODULE=on
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/openset/leetcode

go 1.12
go 1.13
3 changes: 1 addition & 2 deletions internal/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -86,7 +85,7 @@ func JsonIndent(src []byte) []byte {
}

func getFilePath(filename string) string {
if dir := path.Dir(filename); dir != "" {
if dir := filepath.Dir(filename); dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
CheckErr(err)
}
Expand Down
33 changes: 20 additions & 13 deletions internal/description/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,29 @@ func runDescription(cmd *base.Command, args []string) {
cmd.Usage()
}
var wg sync.WaitGroup
tokens := make(chan bool, 1<<7)
limit := 1 << 7
jobs := make(chan *leetcode.StatStatusPairsType, limit)
for i := 0; i < limit; i++ {
go worker(jobs, &wg)
}
problems := leetcode.ProblemsAll()
for _, problem := range problems.StatStatusPairs {
wg.Add(1)
tokens <- true
problem := problem
fmt.Println(problem.Stat.FrontendQuestionId, "\t"+problem.Stat.QuestionTitle)
go func(problem leetcode.StatStatusPairsType) {
titleSlug := problem.Stat.QuestionTitleSlug
question := leetcode.QuestionData(titleSlug, false).Data.Question
if question.Content == "" && problem.PaidOnly == true && problem.Stat.QuestionArticleLive {
question.Content = leetcode.GetDescription(problem.Stat.QuestionArticleSlug)
}
question.SaveContent()
<-tokens
wg.Done()
}(problem)
wg.Add(1)
jobs <- &problem
}
wg.Wait()
}

func worker(jobs <-chan *leetcode.StatStatusPairsType, wg *sync.WaitGroup) {
for problem := range jobs {
titleSlug := problem.Stat.QuestionTitleSlug
question := leetcode.QuestionData(titleSlug, false).Data.Question
if question.Content == "" && problem.PaidOnly == true && problem.Stat.QuestionArticleLive {
question.Content = leetcode.GetDescription(problem.Stat.QuestionArticleSlug)
}
question.SaveContent()
wg.Done()
}
}
4 changes: 2 additions & 2 deletions internal/leetcode/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -60,7 +60,7 @@ func getCachePath(f string) string {
dir, err = os.UserCacheDir()
checkErr(err)
}
return path.Join(dir, ".leetcode", f)
return filepath.Join(dir, ".leetcode", f)
}

func Clean() {
Expand Down
10 changes: 5 additions & 5 deletions internal/leetcode/problems_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type difficultyType struct {

type paidType bool

func (problem StatStatusPairsType) WriteRow(buf *bytes.Buffer) {
func (problem *StatStatusPairsType) WriteRow(buf *bytes.Buffer) {
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"
id := problem.Stat.FrontendQuestionId
stat := problem.Stat
Expand All @@ -79,23 +79,23 @@ func (p paidType) Str() string {
return ""
}

func (s statType) QuestionTitleSnake() string {
func (s *statType) QuestionTitleSnake() string {
return slugToSnake(s.QuestionTitleSlug)
}

func (s statType) TranslationTitle() string {
func (s *statType) TranslationTitle() string {
title := translationSet[s.QuestionId]
if title != "" {
title = fmt.Sprintf(` "%s"`, title)
}
return title
}

func (s statType) Lang() string {
func (s *statType) Lang() string {
return getLang(s.QuestionTitleSlug)
}

func (d difficultyType) LevelName() string {
func (d *difficultyType) LevelName() string {
m := map[int]string{
1: "Easy",
2: "Medium",
Expand Down
40 changes: 20 additions & 20 deletions internal/leetcode/question_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -102,14 +102,14 @@ func (d difficultyStrType) Str() (s string) {
return
}

func (question questionType) SaveContent() {
func (question *questionType) SaveContent() {
if question.TitleSlug != "" {
filePutContents(question.getFilePath("README.md"), question.getDescContent())
question.saveMysqlSchemas()
}
}

func (question questionType) getDescContent() []byte {
func (question *questionType) getDescContent() []byte {
var buf bytes.Buffer
buf.WriteString(authInfo("description"))
buf.WriteString(question.getNavigation())
Expand All @@ -127,7 +127,7 @@ func (question questionType) getDescContent() []byte {
return buf.Bytes()
}

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

func (question questionType) getTopicTags() []byte {
func (question *questionType) getTopicTags() []byte {
tags := question.TopicTags
var buf bytes.Buffer
if len(tags) > 0 {
Expand All @@ -160,12 +160,12 @@ func (question questionType) getTopicTags() []byte {
return buf.Bytes()
}

func (question questionType) GetSimilarQuestion() (sq []similarQuestionType) {
func (question *questionType) GetSimilarQuestion() (sq []similarQuestionType) {
jsonDecode([]byte(question.SimilarQuestions), &sq)
return
}

func (question questionType) getSimilarQuestion() []byte {
func (question *questionType) getSimilarQuestion() []byte {
sq := question.GetSimilarQuestion()
var buf bytes.Buffer
if len(sq) > 0 {
Expand All @@ -178,7 +178,7 @@ func (question questionType) getSimilarQuestion() []byte {
return buf.Bytes()
}

func (question questionType) getHints() []byte {
func (question *questionType) getHints() []byte {
hints := question.Hints
var buf bytes.Buffer
if len(hints) > 0 {
Expand All @@ -190,29 +190,29 @@ func (question questionType) getHints() []byte {
return buf.Bytes()
}

func (question questionType) getFilePath(filename string) string {
return path.Join("problems", question.TitleSlug, filename)
func (question *questionType) getFilePath(filename string) string {
return filepath.Join("problems", question.TitleSlug, filename)
}

func (question questionType) TitleSnake() string {
func (question *questionType) TitleSnake() string {
return slugToSnake(question.TitleSlug)
}

func (question questionType) PackageName() string {
func (question *questionType) PackageName() string {
snake := question.TitleSnake()
if snake != "" && unicode.IsNumber(rune(snake[0])) {
snake = "p_" + snake
}
return snake
}

func (question questionType) SaveCodeSnippet() {
func (question *questionType) SaveCodeSnippet() {
if isLangMySQL(question.TitleSlug) {
filePutContents(question.getFilePath(question.TitleSnake()+".sql"), []byte("# Write your MySQL query statement below\n"))
}
langSupport := [...]struct {
slug string
handle func(questionType, codeSnippetsType)
handle func(*questionType, codeSnippetsType)
}{
{"golang", handleCodeGolang},
{"python3", handleCodePython},
Expand All @@ -234,23 +234,23 @@ func (question questionType) SaveCodeSnippet() {
}
}

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

func (question questionType) saveMysqlSchemas() {
func (question *questionType) saveMysqlSchemas() {
var buf bytes.Buffer
for _, s := range question.MysqlSchemas {
buf.WriteString(s + ";\n")
}
filePutContents(question.getFilePath("mysql_schemas.sql"), buf.Bytes())
}

func handleCodeGolang(question questionType, code codeSnippetsType) {
func handleCodeGolang(question *questionType, code codeSnippetsType) {
content := fmt.Sprintf("package %s\n\n", question.PackageName())
content += code.Code + "\n"
question.saveCodeContent(content, ".go")
Expand All @@ -272,15 +272,15 @@ func handleCodeGolang(question questionType, code codeSnippetsType) {
}
}

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

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

func handleCodeSQL(question questionType, code codeSnippetsType) {
func handleCodeSQL(question *questionType, code codeSnippetsType) {
question.saveCodeContent(code.Code, ".sql")
question.saveMysqlSchemas()
}
Expand Down
12 changes: 6 additions & 6 deletions internal/leetcode/topic_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strconv"
Expand All @@ -16,7 +16,7 @@ import (

var (
initTags []TagType
tagsFile = path.Join("tag", "tags.json")
tagsFile = filepath.Join("tag", "tags.json")
)

func init() {
Expand Down Expand Up @@ -120,7 +120,7 @@ type ttQuestionType struct {
TopicTags []TagType `json:"topicTags"`
}

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

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

func (tag TagType) ShowName() string {
func (tag *TagType) ShowName() string {
if tag.TranslatedName != "" {
return tag.TranslatedName
}
Expand Down
10 changes: 5 additions & 5 deletions internal/post/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -80,8 +80,8 @@ func runPost(cmd *base.Command, args []string) {
buf.WriteString("\n---\n")
buf.WriteString(fmt.Sprintf("\n## [答案](https://github.com/openset/leetcode/tree/master/problems/%s)\n", question.TitleSlug))
filename := fmt.Sprintf(formatFilename, t.Format("2006-01-02"), question.TitleSlug)
oldPath := path.Join(basePath, "leetcode", filename)
newPath := path.Join(basePath, "_posts", filename)
oldPath := filepath.Join(basePath, "leetcode", filename)
newPath := filepath.Join(basePath, "_posts", filename)
base.FilePutContents(oldPath, buf.Bytes())
if inPosts[questionId] {
_ = os.Rename(oldPath, newPath)
Expand All @@ -101,7 +101,7 @@ func postTags() {
}
filename := fmt.Sprintf("tag-%s.md", tag.Slug)
data := []byte(fmt.Sprintf(tagTmpl, title, tag.Slug, tag.Name))
base.FilePutContents(path.Join(basePath, "_pages", filename), data)
base.FilePutContents(filepath.Join(basePath, "_pages", filename), data)
}
}

Expand All @@ -125,7 +125,7 @@ taxonomy: %s

var homeDir, _ = os.UserHomeDir()

var basePath = path.Join(homeDir, "openset", "openset")
var basePath = filepath.Join(homeDir, "openset", "openset")

var inPosts = map[int]bool{
1: true,
Expand Down
4 changes: 2 additions & 2 deletions internal/readme/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package readme
import (
"bytes"
"fmt"
"path"
"path/filepath"

"github.com/openset/leetcode/internal/base"
"github.com/openset/leetcode/internal/leetcode"
Expand Down Expand Up @@ -47,7 +47,7 @@ func writeProblems(buf *bytes.Buffer) {
count--
problems[count].WriteRow(buf)
}
fileName := path.Join("readme", fmt.Sprintf("%d-%d.md", pageSize*(i-1)+1, pageSize*i))
fileName := filepath.Join("readme", fmt.Sprintf("%d-%d.md", pageSize*(i-1)+1, pageSize*i))
base.FilePutContents(fileName, buf.Bytes())
buf.Truncate(n)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/openset/leetcode/internal/base"
)

const version = "1.4.4"
const version = "1.4.5"

var CmdVersion = &base.Command{
Run: runVersion,
Expand Down