Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc59511

Browse files
author
Sandy
authored
Merge pull request #25 from openset/develop
Add: CmdReadme
2 parents 908a49d + 5c5b05a commit dc59511

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

‎internal/base/base.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ func Usage() {
4545
Exit()
4646
}
4747

48+
func CheckErr(err error) {
49+
if err != nil {
50+
panic(err)
51+
}
52+
}
53+
4854
func Exit() {
4955
os.Exit(0)
5056
}

‎internal/readme/readme.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
[![Build Status](https://travis-ci.org/openset/leetcode.svg?branch=master)](https://travis-ci.org/openset/leetcode)
59+
[![codecov](https://codecov.io/gh/openset/leetcode/branch/master/graph/badge.svg)](https://codecov.io/gh/openset/leetcode)
60+
[![Go Report Card](https://goreportcard.com/badge/github.com/openset/leetcode)](https://goreportcard.com/report/github.com/openset/leetcode)
61+
[![GitHub contributors](https://img.shields.io/github/contributors/openset/leetcode.svg)](https://github.com/openset/leetcode/graphs/contributors)
62+
[![license](https://img.shields.io/github/license/openset/leetcode.svg)](https://github.com/openset/leetcode/blob/master/LICENSE)
63+
[![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/openset/leetcode.svg?colorB=green)](https://github.com/openset/leetcode/archive/master.zip)
64+
65+
| # | Title | Solution | Difficulty |
66+
| :-: | - | - | :-: |
67+
`

‎main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77

88
"github.com/openset/leetcode/internal/base"
99
"github.com/openset/leetcode/internal/help"
10+
"github.com/openset/leetcode/internal/readme"
1011
"github.com/openset/leetcode/internal/version"
1112
)
1213

1314
func init() {
1415
base.Commands = []*base.Command{
16+
readme.CmdReadme,
1517
version.CmdVersion,
1618
help.CmdHelp,
1719
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /