Skip to content

Commit 90ca664

Browse files
committed
misc: add analysis scripts
1 parent 5b40f04 commit 90ca664

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ out/
99
go.work
1010
go.work.sum
1111
misc/*.gif
12+
misc/questions.json

scripts/analyze.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/goccy/go-json"
9+
10+
"github.com/j178/leetgo/config"
11+
"github.com/j178/leetgo/lang"
12+
"github.com/j178/leetgo/leetcode"
13+
)
14+
15+
func main() {
16+
f, err := os.Open("misc/questions.json")
17+
if err != nil {
18+
panic(err)
19+
}
20+
defer f.Close()
21+
22+
_ = os.Chdir(os.Getenv("LEETGO_WORKDIR"))
23+
err = config.Load(false)
24+
if err != nil {
25+
panic(err)
26+
}
27+
28+
var questions []*leetcode.QuestionData
29+
err = json.NewDecoder(f).Decode(&questions)
30+
if err != nil {
31+
panic(err)
32+
}
33+
c := leetcode.NewClient(leetcode.NonAuth())
34+
35+
categories := map[leetcode.CategoryTitle]int{}
36+
for _, q := range questions {
37+
q.SetClient(c)
38+
39+
categories[q.CategoryTitle]++
40+
if q.MetaData.Manual && q.CategoryTitle == leetcode.CategoryAlgorithms {
41+
fmt.Printf("%s.%s\n", q.QuestionFrontendId, q.TitleSlug)
42+
out, err := lang.Generate(q)
43+
if err != nil {
44+
fmt.Println(err)
45+
}
46+
f, _ := os.Create(filepath.Join(out.TargetDir(), "question.json"))
47+
enc := json.NewEncoder(f)
48+
enc.SetIndent("", " ")
49+
enc.Encode(q)
50+
f.Close()
51+
}
52+
}
53+
54+
fmt.Printf(
55+
"total: %d, %v, manual: %d\n",
56+
len(questions),
57+
categories,
58+
)
59+
}

scripts/fetch_question_data.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
8+
"github.com/goccy/go-json"
9+
10+
"github.com/j178/leetgo/leetcode"
11+
)
12+
13+
func main() {
14+
client := leetcode.NewClient(leetcode.ReadCredentials())
15+
cache := leetcode.GetCache(client)
16+
questions := cache.GetAllQuestions()
17+
paidOnly := 0
18+
for _, q := range questions {
19+
if q.IsPaidOnly {
20+
paidOnly++
21+
}
22+
}
23+
fmt.Printf("Total questions: %d, paid only: %d\n", len(questions), paidOnly)
24+
25+
for i, q := range questions {
26+
if q.IsPaidOnly {
27+
continue
28+
}
29+
err := q.Fulfill()
30+
if err != nil {
31+
fmt.Printf("fetch error: %s, q=%s\n", err, q.TitleSlug)
32+
continue
33+
}
34+
if i > 0 && i%100 == 0 {
35+
fmt.Printf("\rfetching %d/%d", i+1, len(questions))
36+
save(questions)
37+
}
38+
time.Sleep(10 * time.Millisecond)
39+
}
40+
fmt.Println("\nDone")
41+
}
42+
43+
func save(questions []*leetcode.QuestionData) {
44+
f, err := os.Create("./misc/questions.json")
45+
if err != nil {
46+
panic(err)
47+
}
48+
defer f.Close()
49+
enc := json.NewEncoder(f)
50+
enc.SetIndent("", " ")
51+
err = enc.Encode(questions)
52+
if err != nil {
53+
panic(err)
54+
}
55+
}

0 commit comments

Comments
 (0)