|
| 1 | +package readme |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/openset/leetcode/internal/base" |
| 10 | + "github.com/openset/leetcode/internal/leetcode" |
| 11 | +) |
| 12 | + |
| 13 | +var CmdReadme = &base.Command{ |
| 14 | + Run: runReadme, |
| 15 | + UsageLine: "readme", |
| 16 | + Short: "build README.md file", |
| 17 | + Long: "build README.md file.", |
| 18 | +} |
| 19 | + |
| 20 | +func runReadme(cmd *base.Command, args []string) { |
| 21 | + if len(args) != 0 { |
| 22 | + cmd.Usage() |
| 23 | + } |
| 24 | + wb := bytes.NewBuffer(nil) |
| 25 | + wb.WriteString(defaultStr) |
| 26 | + writeProblems(wb) |
| 27 | + err := ioutil.WriteFile("README.md", wb.Bytes(), 0644) |
| 28 | + base.CheckErr(err) |
| 29 | +} |
| 30 | + |
| 31 | +func writeProblems(wb *bytes.Buffer) { |
| 32 | + problems := leetcode.ProblemsAll() |
| 33 | + problemsSet := make(map[int]string) |
| 34 | + maxId := 0 |
| 35 | + for _, problem := range problems.StatStatusPairs { |
| 36 | + id := problem.Stat.FrontendQuestionId |
| 37 | + title := strings.TrimSpace(problem.Stat.QuestionTitle) |
| 38 | + slug := problem.Stat.QuestionTitleSlug |
| 39 | + levelName := problem.Difficulty.LevelName() |
| 40 | + format := "| %d | [%s](https://leetcode.com/problems/%s) | [Go](https://github.com/openset/leetcode/tree/master/solution/%s) | %s |\n" |
| 41 | + problemsSet[id] = fmt.Sprintf(format, id, title, slug, slug, levelName) |
| 42 | + if id > maxId { |
| 43 | + maxId = id |
| 44 | + } |
| 45 | + } |
| 46 | + for i := 0; i <= maxId; i++ { |
| 47 | + if row, ok := problemsSet[i]; ok { |
| 48 | + wb.WriteString(row) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +const defaultStr = `<!--This file generated by command(leetcode readme); DO NOT EDIT.--> |
| 54 | + |
| 55 | +# [LeetCode](https://openset.github.io/leetcode) |
| 56 | +LeetCode Problems' Solutions |
| 57 | + |
| 58 | +[](https://travis-ci.org/openset/leetcode) |
| 59 | +[](https://codecov.io/gh/openset/leetcode) |
| 60 | +[](https://goreportcard.com/report/github.com/openset/leetcode) |
| 61 | +[](https://github.com/openset/leetcode/graphs/contributors) |
| 62 | +[](https://github.com/openset/leetcode/blob/master/LICENSE) |
| 63 | +[](https://github.com/openset/leetcode/archive/master.zip) |
| 64 | + |
| 65 | +| # | Title | Solution | Difficulty | |
| 66 | +| :-: | - | - | :-: | |
| 67 | +` |
0 commit comments