Skip to content

Commit f0d8201

Browse files
committed
update
1 parent 5e11aa7 commit f0d8201

File tree

15 files changed

+339
-87
lines changed

15 files changed

+339
-87
lines changed

copypasta/template/leetcode/generator_test.go

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,57 @@ func writeMainFile(problemID, defaultCode string) error {
9999
}
100100

101101
func writeTestFile(problemID, funcName string, sampleIns, sampleOuts [][]string) error {
102-
if funcName == "" {
103-
return fmt.Errorf("funcName is empty")
104-
}
105102
if len(sampleIns) != len(sampleOuts) {
106103
return fmt.Errorf("len(sampleIns) != len(sampleOuts) : %d != %d", len(sampleIns), len(sampleOuts))
107104
}
108-
109105
funcName = strings.TrimSpace(funcName)
110-
sampleToStr := func(samples [][]string) (s string) {
111-
for i, args := range samples {
112-
if i > 0 {
113-
s += ", "
106+
107+
var testStr string
108+
if funcName == "" {
109+
// 编写类
110+
sampleToStr := func(samples [][]string) (s string) {
111+
for _, text := range samples {
112+
s += text[0]
114113
}
115-
s += "{"
116-
for j, arg := range args {
117-
if j > 0 {
114+
return
115+
}
116+
testStr = fmt.Sprintf(`package main
117+
118+
import (
119+
"github.com/stretchr/testify/assert"
120+
"strings"
121+
"testing"
122+
)
123+
124+
func Test(t *testing.T) {
125+
t.Log("Current test is [%s]")
126+
// copy to the Custom Testcase
127+
const exampleIns = `+"`\n%s`"+`
128+
exampleOuts := `+"`\n%s`"+`
129+
// copy Your answer in the Run Code Result
130+
yourAnswers := `+"`\n\n`"+`
131+
assert.Equal(t, strings.TrimSpace(exampleOuts), strings.TrimSpace(yourAnswers))
132+
}
133+
`, problemID, sampleToStr(sampleIns), sampleToStr(sampleOuts))
134+
} else {
135+
// 编写函数
136+
sampleToStr := func(samples [][]string) (s string) {
137+
for i, args := range samples {
138+
if i > 0 {
118139
s += ", "
119140
}
120-
s += "`" + arg + "`"
141+
s += "{"
142+
for j, arg := range args {
143+
if j > 0 {
144+
s += ", "
145+
}
146+
s += "`" + arg + "`"
147+
}
148+
s += "}"
121149
}
122-
s += "}"
150+
return
123151
}
124-
return
125-
}
126-
testStr := fmt.Sprintf(`// Code generated by generator_test.
152+
testStr = fmt.Sprintf(`// Code generated by generator_test.
127153
package main
128154
129155
import (
@@ -133,13 +159,18 @@ import (
133159
134160
func Test(t *testing.T) {
135161
t.Log("Current test is [%s]")
136-
sampleIns := [][]string{%s}
137-
sampleOuts := [][]string{%s}
138-
if err := testutil.RunLeetCodeFunc(t, %s, sampleIns, sampleOuts); err != nil {
162+
exampleIns := [][]string{%s}
163+
exampleOuts := [][]string{%s}
164+
// custom test cases or WA cases.
165+
//exampleIns = append(exampleIns, []string{`+"``"+`})
166+
//exampleOuts = append(exampleOuts, []string{`+"``"+`})
167+
if err := testutil.RunLeetCodeFunc(t, %s, exampleIns, exampleOuts); err != nil {
139168
t.Fatal(err)
140169
}
141170
}
142171
`, problemID, sampleToStr(sampleIns), sampleToStr(sampleOuts), funcName)
172+
}
173+
143174
filePath := contestDir + fmt.Sprintf("%[1]s/%[1]s_test.go", problemID)
144175
return ioutil.WriteFile(filePath, []byte(testStr), 0644)
145176
}
@@ -179,8 +210,9 @@ func parseHTML(fileName string, htmlURL string) error {
179210
}
180211
}
181212

213+
// 解析模板(codeDefinition)
182214
var funcName string
183-
var genTestFile bool
215+
var isFuncProblem bool // 编写函数还是类
184216
for o := bodyNode.FirstChild; o != nil; o = o.NextSibling {
185217
if o.Type == html.ElementNode && o.Data == "script" && o.FirstChild != nil {
186218
jsText := o.FirstChild.Data
@@ -201,7 +233,7 @@ func parseHTML(fileName string, htmlURL string) error {
201233

202234
for _, e := range d {
203235
if e.Value == "golang" {
204-
funcName, genTestFile = parseFuncName(e.DefaultCode)
236+
funcName, isFuncProblem = parseFuncName(e.DefaultCode)
205237
if err := writeMainFile(fileName, e.DefaultCode); err != nil {
206238
return err
207239
}
@@ -213,10 +245,7 @@ func parseHTML(fileName string, htmlURL string) error {
213245
}
214246
}
215247

216-
if !genTestFile {
217-
return nil
218-
}
219-
248+
// 解析样例输入输出
220249
const (
221250
tokenInputZH = "输入:"
222251
tokenOutputZH = "输出:"
@@ -233,6 +262,9 @@ func parseHTML(fileName string, htmlURL string) error {
233262
text = ""
234263
for _, l := range lines {
235264
text += strings.TrimSpace(l)
265+
if !isFuncProblem {
266+
text += "\n"
267+
}
236268
}
237269

238270
if !isASCII(text) {
@@ -252,19 +284,24 @@ func parseHTML(fileName string, htmlURL string) error {
252284
sample = append(sample, strings.TrimSpace(s[:end]))
253285
}
254286
sample = append(sample, strings.TrimSpace(splits[len(splits)-1]))
287+
288+
if !isFuncProblem {
289+
sample = []string{strings.Join(sample, "\n") + "\n"}
290+
}
291+
255292
return
256293
}
257294
var f func(*html.Node)
258295
f = func(o *html.Node) {
259296
if o.Type == html.TextNode {
260-
if o.Data == tokenInputZH {
297+
if strings.Contains(o.Data, tokenInputZH) {
261298
raw := o.Parent.NextSibling.Data
262299
sample := parseSampleText(raw, true)
263300
if sample == nil {
264301
fmt.Fprintf(os.Stderr, "错误的输入数据:%s\n", raw)
265302
}
266303
sampleIns = append(sampleIns, sample)
267-
} else if o.Data == tokenOutputZH {
304+
} else if strings.Contains(o.Data, tokenOutputZH) {
268305
raw := o.Parent.NextSibling.Data
269306
sample := parseSampleText(raw, true)
270307
if sample == nil {

copypasta/template/leetcode/helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"unicode"
66
)
77

8-
func parseFuncName(code string) (string, bool) {
8+
func parseFuncName(code string) (funcName string, isFuncProblem bool) {
99
code = strings.TrimSpace(code)
1010
if !strings.HasPrefix(code, "func ") {
11-
return "", false
11+
return
1212
}
1313
i := strings.IndexByte(code, '(')
1414
return code[5:i], true

leetcode/146/a/a.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "sort"
4+
5+
func numEquivDominoPairs(dominoes [][]int) int {
6+
cnt := [81]int{}
7+
for _, d := range dominoes {
8+
sort.Ints(d)
9+
cnt[9*(d[0]-1)+d[1]-1]++
10+
}
11+
ans := 0
12+
for _, c := range cnt {
13+
if c > 1 {
14+
ans += c * (c - 1) / 2
15+
}
16+
}
17+
return ans
18+
}

leetcode/146/a/a_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/leetcode/testutil"
5+
"testing"
6+
)
7+
8+
func Test(t *testing.T) {
9+
t.Log("Current test is [a]")
10+
sampleIns := [][]string{{`[[1,2],[2,1],[3,4],[5,6]]`}}
11+
sampleOuts := [][]string{{`1`}}
12+
if err := testutil.RunLeetCodeFunc(t, numEquivDominoPairs, sampleIns, sampleOuts); err != nil {
13+
t.Fatal(err)
14+
}
15+
}

leetcode/146/b/b.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
func shortestAlternatingPaths(n int, redEdges [][]int, blueEdges [][]int) []int {
4+
min := func(a, b int) int {
5+
if a < b {
6+
return a
7+
}
8+
return b
9+
}
10+
type pair struct {
11+
v int
12+
isBlue bool
13+
length int
14+
}
15+
g := make([][]pair, n)
16+
for _, p := range redEdges {
17+
g[p[0]] = append(g[p[0]], pair{p[1], false, 0})
18+
}
19+
for _, p := range blueEdges {
20+
g[p[0]] = append(g[p[0]], pair{p[1], true, 0})
21+
}
22+
23+
ans := make([]int, n)
24+
for i := 1; i < n; i++ {
25+
ans[i] = -1
26+
}
27+
visB := make([]bool, n)
28+
visR := make([]bool, n)
29+
q := []pair{{}}
30+
for len(q) > 0 {
31+
var p pair
32+
p, q = q[0], q[1:]
33+
for _, e := range g[p.v] {
34+
w, isBlue := e.v, e.isBlue
35+
if p.v > 0 && isBlue == p.isBlue {
36+
continue
37+
}
38+
if isBlue && visB[w] || !isBlue && visR[w] {
39+
continue
40+
}
41+
if isBlue {
42+
visB[w] = true
43+
} else {
44+
visR[w] = true
45+
}
46+
if ans[w] == -1 {
47+
ans[w] = p.length + 1
48+
} else {
49+
ans[w] = min(ans[w], p.length+1)
50+
}
51+
q = append(q, pair{w, isBlue, p.length + 1})
52+
}
53+
}
54+
return ans
55+
}

leetcode/146/b/b_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/leetcode/testutil"
5+
"testing"
6+
)
7+
8+
func Test(t *testing.T) {
9+
t.Log("Current test is [b]")
10+
sampleIns := [][]string{{`3`, `[[0,1],[1,2]]`, `[]`}, {`3`, `[[0,1]]`, `[[2,1]]`}, {`3`, `[[1,0]]`, `[[2,1]]`}, {`3`, `[[0,1]]`, `[[1,2]]`}, {`3`, `[[0,1],[0,2]]`, `[[1,0]]`}}
11+
sampleOuts := [][]string{{`[0,1,-1]`}, {`[0,1,-1]`}, {`[0,-1,-1]`}, {`[0,1,2]`}, {`[0,1,1]`}}
12+
if err := testutil.RunLeetCodeFunc(t, shortestAlternatingPaths, sampleIns, sampleOuts); err != nil {
13+
t.Fatal(err)
14+
}
15+
}

leetcode/146/d/d.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
func maxAbsValExpr(a1 []int, a2 []int) int {
4+
min := func(a, b int) int {
5+
if a < b {
6+
return a
7+
}
8+
return b
9+
}
10+
max := func(a, b int) int {
11+
if a > b {
12+
return a
13+
}
14+
return b
15+
}
16+
const inf int = 1e9
17+
mins := [4]int{inf, inf, inf, inf}
18+
maxs := [4]int{-inf, -inf, -inf, -inf}
19+
for i := range a1 {
20+
for j, v := range [4]int{
21+
a1[i] + a2[i] + i,
22+
a1[i] - a2[i] + i,
23+
-a1[i] + a2[i] + i,
24+
-a1[i] - a2[i] + i,
25+
} {
26+
mins[j] = min(mins[j], v)
27+
maxs[j] = max(maxs[j], v)
28+
}
29+
}
30+
ans := -inf
31+
for i := range mins {
32+
ans = max(ans, maxs[i]-mins[i])
33+
}
34+
return ans
35+
}

leetcode/146/d/d_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/leetcode/testutil"
5+
"testing"
6+
)
7+
8+
func Test(t *testing.T) {
9+
t.Log("Current test is [d]")
10+
sampleIns := [][]string{{`[1,2,3,4]`, `[-1,4,5,6]`}, {`[1,-2,-5,0,10]`, `[0,-2,-1,-7,-4]`}}
11+
sampleOuts := [][]string{{`13`}, {`20`}}
12+
if err := testutil.RunLeetCodeFunc(t, maxAbsValExpr, sampleIns, sampleOuts); err != nil {
13+
t.Fatal(err)
14+
}
15+
}

leetcode/150/b/b_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func Test(t *testing.T) {
10+
t.Log("Current test is [b]")
11+
// copy to the Custom Testcase
12+
const exampleIns = `
13+
[1,7,0,7,-8,null,null]
14+
`
15+
exampleOuts := `
16+
2
17+
`
18+
// copy Your answer in the Run Code Result
19+
yourAnswers := `
20+
2
21+
`
22+
assert.Equal(t, strings.TrimSpace(exampleOuts), strings.TrimSpace(yourAnswers))
23+
}

leetcode/151/c/c_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func Test(t *testing.T) {
10+
t.Log("Current test is [c]")
11+
// copy to the Custom Testcase
12+
const exampleIns = `
13+
[1,2,-3,3,1]
14+
[1,2,3,-3,4]
15+
[1,2,3,-3,-2]
16+
`
17+
exampleOuts := `
18+
[3,1]
19+
[1,2,4]
20+
[1]
21+
`
22+
// copy Your answer in the Run Code Result
23+
yourAnswers := `
24+
[3,1]
25+
[1,2,4]
26+
[1]
27+
`
28+
assert.Equal(t, strings.TrimSpace(exampleOuts), strings.TrimSpace(yourAnswers))
29+
}

0 commit comments

Comments
 (0)