Skip to content

Commit 85bb5a3

Browse files
committed
add some tests and go fmt
1 parent 29d7574 commit 85bb5a3

12 files changed

+152
-11
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test:
2+
go test ./...

leetcode/3sum.go renamed to leetcode/3sum/3sum.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/3sum/
2-
package main
2+
package _sum
33

44
import (
55
"sort"

leetcode/3sum/3sum_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package _sum
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_threeSum(t *testing.T) {
9+
cases := []struct {
10+
params []int
11+
exp [][]int
12+
}{
13+
{
14+
[]int{-1, 0, 1, 2, -1, -4}, [][]int{{-1, -1, 2}, {-1, 0, 1}},
15+
},
16+
{
17+
[]int{-1, 0, 1, 2, -1, -4}, [][]int{{-1, -1, 2}, {-1, 0, 1}},
18+
},
19+
{
20+
[]int{-1, 0, 1, 2, -1, -4}, [][]int{{-1, -1, 2}, {-1, 0, 1}},
21+
},
22+
}
23+
24+
for idx, tc := range cases {
25+
out := threeSum(tc.params)
26+
if !reflect.DeepEqual(out, tc.exp) {
27+
t.Errorf("case %d error: got %v, expected %v", idx, out, tc.exp)
28+
}
29+
}
30+
}

leetcode/accounts_merge.go renamed to leetcode/accounts_merge/accounts_merge.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/accounts-merge/
2-
package main
2+
package accounts_merge
33

44
import (
55
"sort"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package accounts_merge
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_accountsMerge(t *testing.T) {
9+
cases := []struct {
10+
params [][]string
11+
exp [][]string
12+
}{
13+
{
14+
[][]string{{"John", "[email protected]", "[email protected]"}, {"John", "[email protected]", "[email protected]"}, {"Mary", "[email protected]"}, {"John", "[email protected]"}},
15+
[][]string{{"John", "[email protected]", "[email protected]", "[email protected]"}, {"Mary", "[email protected]"}, {"John", "[email protected]"}},
16+
},
17+
{
18+
19+
20+
},
21+
}
22+
23+
for idx, tc := range cases {
24+
out := accountsMerge(tc.params)
25+
if !reflect.DeepEqual(out, tc.exp) {
26+
t.Errorf("case %d error: \n-- got: %v\n-- expected: %v", idx, out, tc.exp)
27+
}
28+
}
29+
}

leetcode/add_and_search_word_data_structure_design.go renamed to leetcode/add_and_search_word_data_structure_design/add_and_search_word_data_structure_design.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/add-and-search-word-data-structure-design/
2-
package main
2+
package add_and_search_word_data_structure_design
33

44
type WordDictionary struct {
55
root *Node
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package add_and_search_word_data_structure_design
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestWordDictionary(t *testing.T) {
8+
wd := Constructor()
9+
wd.AddWord("bad")
10+
wd.AddWord("dad")
11+
wd.AddWord("mad")
12+
assert(t, wd.Search("pad") == false)
13+
assert(t, wd.Search("bad"))
14+
assert(t, wd.Search(".ad"))
15+
assert(t, wd.Search("b.."))
16+
}
17+
18+
func assert(t *testing.T, res bool) {
19+
if !res {
20+
t.Errorf("assert fails")
21+
}
22+
}

leetcode/add_binary.go renamed to leetcode/add_binary/add_binary.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/add-binary/
2-
package main
2+
package add_binary
33

44
func addBinary(a string, b string) string {
55
aIdxShift := 0
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package add_binary
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_addBinary(t *testing.T) {
9+
cases := []struct {
10+
params []string
11+
exp string
12+
}{
13+
{
14+
[]string{"11", "1"},
15+
"100",
16+
},
17+
{
18+
[]string{"1010", "1011"},
19+
"10101",
20+
},
21+
}
22+
23+
for idx, tc := range cases {
24+
out := addBinary(tc.params[0], tc.params[1])
25+
if !reflect.DeepEqual(out, tc.exp) {
26+
t.Errorf("case %d error: got: %v, expected: %v", idx, out, tc.exp)
27+
}
28+
}
29+
}

leetcode/add_strings.go renamed to leetcode/add_strings/add_strings.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/add-strings/
2-
package main
2+
package add_strings
33

44
func addStrings(num1 string, num2 string) string {
55
total := make([]byte, max(len(num1), len(num2))+1)
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package add_strings
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_addBinary(t *testing.T) {
9+
cases := []struct {
10+
params []string
11+
exp string
12+
}{
13+
{
14+
[]string{"11", "123"},
15+
"134",
16+
},
17+
{
18+
[]string{"456", "77"},
19+
"533",
20+
},
21+
{
22+
[]string{"0", "0"},
23+
"0",
24+
},
25+
}
26+
27+
for idx, tc := range cases {
28+
out := addStrings(tc.params[0], tc.params[1])
29+
if !reflect.DeepEqual(out, tc.exp) {
30+
t.Errorf("case %d error: got: %v, expected: %v", idx, out, tc.exp)
31+
}
32+
}
33+
}

leetcode/problems_remove_nth_node_from_end_of_list.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ package main
1212
func removeNthFromEnd(head *ListNode, n int) *ListNode {
1313
current := head
1414
cnt := 0
15-
for ;current!=nil; {
15+
for current != nil {
1616
current = current.Next
1717
cnt++
1818
}
1919

20-
21-
2220
nPos := cnt - n - 1
2321
if nPos == -1 {
2422
return head.Next
@@ -27,9 +25,8 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
2725
return nil
2826
}
2927

30-
3128
current = head
32-
for i:=0; i<nPos; i++ {
29+
for i := 0; i < nPos; i++ {
3330
current = current.Next
3431
}
3532

@@ -39,4 +36,3 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
3936

4037
return head
4138
}
42-

0 commit comments

Comments
 (0)