Skip to content

Commit

Permalink
more linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Apr 6, 2019
1 parent 3b2def6 commit 6e55aef
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
6 changes: 3 additions & 3 deletions day134/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type SparseArray struct {
size int
}

var errIndexOutOfRange = fmt.Errorf("Index is out of range")
var errIndexOutOfRange = fmt.Errorf("index is out of range")

// ErrIndexOutOfRange returns the error that's associated with access beyond
// the limits of the SparseArray.
Expand All @@ -31,7 +31,7 @@ func NewSparseArray(arr []int) *SparseArray {
// Set mutates the value at position 'i' with value 'val'.
func (sa *SparseArray) Set(i, val int) error {
if sa == nil {
return fmt.Errorf("Create with NewSparseArray")
return fmt.Errorf("create with NewSparseArray")
}
if i >= sa.size || i < 0 {
return errIndexOutOfRange
Expand All @@ -47,7 +47,7 @@ func (sa *SparseArray) Set(i, val int) error {
// Get returns the value at position 'i'.
func (sa *SparseArray) Get(i int) (int, error) {
if sa == nil {
return 0, fmt.Errorf("Create with NewSparseArray")
return 0, fmt.Errorf("create with NewSparseArray")
}
if i >= sa.size || i < 0 {
return 0, errIndexOutOfRange
Expand Down
13 changes: 10 additions & 3 deletions day134/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ func TestSparseArray(t *testing.T) {
t.Errorf("Expected (%v,%v) got (%v,%v)", val, nil, result, err)
}
}
sa.Set(0, 10)
if err := sa.Set(0, 10); err != nil {
t.Error("set shouldn't return an error", err)
}
if result, err := sa.Get(0); result != 10 || err != nil {
t.Errorf("Set didn't work properly")
t.Error("Set didn't work properly")
}
if err := sa.Set(0, 0); err != nil {
t.Error("set shouldn't return an error", err)
}
sa.Set(0, 0)
for i, val := range arr {
if result, err := sa.Get(i); result != val || err != nil {
t.Errorf("Expected (%v,%v) got (%v,%v)", val, nil, result, err)
Expand Down Expand Up @@ -51,8 +55,11 @@ func BenchmarkSparseArray(b *testing.B) {
arr := []int{0, 0, 0, 0, 9, 8, 7, 6, 0, 0}
sa := NewSparseArray(arr)
for i := 0; i < b.N; i++ {
//nolint: errcheck
sa.Set(0, 10)
//nolint: errcheck
sa.Get(0)
//nolint: errcheck
sa.Set(0, 0)

}
Expand Down
2 changes: 1 addition & 1 deletion day188/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ func MakeFunctionsOriginal() []func() int {
var funcList []func() int
for _, i := range []int{1, 2, 3} {
funcList = append(funcList, func() int {
return i
return i //nolint: scopelint
})
}
return funcList
Expand Down
12 changes: 6 additions & 6 deletions day20/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ func createTwoSharedLists() (*Node, *Node, *Node) {
return one, two, common
}

func createTwoIndependentLists() (*Node, *Node, *Node) {
func createTwoIndependentLists() (*Node, *Node) {
one := &Node{"abc", &Node{"def", &Node{"ghi", nil}}}
two := &Node{111, nil}
return one, two, nil
return one, two
}

func TestFindCommonNode(t *testing.T) {
Expand All @@ -27,19 +27,19 @@ func TestFindCommonNode(t *testing.T) {
if result := FindCommonNode(one, two); result != common {
t.Error("Expected these two be the same pointer")
}
one, two, common = createTwoIndependentLists()
if result := FindCommonNode(one, two); result != common {
one, two = createTwoIndependentLists()
if result := FindCommonNode(one, two); result != nil {
t.Error("Expected these two be nil")
}
one, two = two, one // swap
if result := FindCommonNode(one, two); result != common {
if result := FindCommonNode(one, two); result != nil {
t.Error("Expected these two be nil")
}
}

func BenchmarkFindCommonNode(b *testing.B) {
one, two, _ := createTwoSharedLists()
onei, twoi, _ := createTwoIndependentLists()
onei, twoi := createTwoIndependentLists()
b.ResetTimer()
for i := 0; i < b.N; i++ {
FindCommonNode(one, two)
Expand Down
9 changes: 5 additions & 4 deletions day87/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ func IsValidRules(rules []string) bool {
toPos, toFound := positions[to]
fromPos, fromFound := positions[from]
delta := directions[dir]
if toFound && fromFound {
switch {
case toFound && fromFound:
if !checkValid(fromPos, toPos, dir) {
return false
}
} else if toFound && !fromFound {
case toFound && !fromFound:
positions[from] = Position{toPos.X - delta.X, toPos.Y - delta.Y}
} else if !toFound && fromFound {
case !toFound && fromFound:
positions[to] = Position{fromPos.X + delta.X, fromPos.Y + delta.Y}
} else {
default:
positions[from] = Position{0, 0}
positions[to] = directions[dir]
}
Expand Down
7 changes: 4 additions & 3 deletions day9/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ package day9
func MaximumNonAdjacentSum(nums []int) int {
var one, two, result int
for i, val := range nums {
if i == 0 {
switch {
case i == 0:
result = val
} else if i == 1 {
case i == 1:
result = max(result, val)
} else {
default:
result = max(one, val+two)
}
two = one
Expand Down
2 changes: 1 addition & 1 deletion day91/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ func BadSnippet() []int {
funcs := make([]func() int, 9)
for i := 0; i < 9; i++ {
funcs[i] = func() int {
return i
return i //nolint: scopelint
}
}
result := make([]int, 9)
Expand Down

0 comments on commit 6e55aef

Please sign in to comment.