Skip to content

Commit

Permalink
Upgrade Go, use generics instead of interface, use slice.Equal and in…
Browse files Browse the repository at this point in the history
…t range (#137)
  • Loading branch information
spring1843 committed Jun 28, 2024
1 parent cb47c27 commit a261c65
Show file tree
Hide file tree
Showing 60 changed files with 149 additions and 159 deletions.
10 changes: 3 additions & 7 deletions .github/linters/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ issues:
exclude-rules:
- path: /
linters:
- nosnakecase
- nlreturn
- forcetypeassert
- gci
- gomnd
- ifshort
- scopelint
- forcetypeassert
- varnamelen
- lll
- testpackage
- gomnd
- wsl
- gochecknoglobals
- paralleltest
Expand All @@ -34,9 +33,6 @@ issues:
- dupword
linters:
enable-all: true
enable:
- revive
- misspell

linters-settings:
forbidigo:
Expand Down
15 changes: 4 additions & 11 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: reviewdog/action-misspell@v1
with:
github_token: ${{ secrets.github_token }}
locale: "US"
- uses: github/super-linter@v4
- uses: github/super-linter@v5
env:
DEFAULT_BRANCH: main
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -30,15 +30,8 @@ jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/
~/go/bin/
key: go-cache-${{ hashFiles('go.mod') }}
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- run: go test -v -coverprofile=profile.cov ./...
- uses: shogo82148/actions-goveralls@v1
with:
Expand Down
5 changes: 1 addition & 4 deletions array/add_two_numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ func AddTwoNumbers(num1, num2 []int) []int {

func equalizeLengths(num1, num2 []int) ([]int, []int) {
diff := int(math.Abs(float64(len(num2) - len(num1))))
zeros := []int{}
for i := 0; i < diff; i++ {
zeros = append(zeros, 0)
}
zeros := make([]int, diff)
if len(num2) > len(num1) {
num1 = append(zeros, num1...)
} else if len(num1) > len(num2) {
Expand Down
4 changes: 2 additions & 2 deletions array/add_two_numbers_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -26,7 +26,7 @@ func TestAddTwoNumbers(t *testing.T) {
{[]int{9, 9, 9}, []int{9, 9, 9}, []int{1, 9, 9, 8}},
}
for i, test := range tests {
if got := AddTwoNumbers(test.num1, test.num2); !reflect.DeepEqual(got, test.sum) {
if got := AddTwoNumbers(test.num1, test.num2); !slices.Equal(got, test.sum) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sum, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions array/bubble_sort_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestBubbleSort(t *testing.T) {
}
for i, test := range tests {
BubbleSort(test.input)
if !reflect.DeepEqual(test.input, test.sorted) {
if !slices.Equal(test.input, test.sorted) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, test.input)
}
}
Expand Down
2 changes: 1 addition & 1 deletion array/equal_sum_subarrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func EqualSubArrays(list []int) [][]int {

func findSplitPoint(list []int) int {
lSum := 0
for i := 0; i < len(list); i++ {
for i := range len(list) {
lSum += list[i]

rSum := 0
Expand Down
4 changes: 2 additions & 2 deletions array/insertion_sort_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -28,7 +28,7 @@ func TestInsertionSort(t *testing.T) {
}
for i, test := range tests {
InsertionSort(test.input)
if !reflect.DeepEqual(test.input, test.sorted) {
if !slices.Equal(test.input, test.sorted) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, test.input)
}
}
Expand Down
4 changes: 2 additions & 2 deletions array/product_of_all_other_elements_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -26,7 +26,7 @@ func TestProductOfAllOtherElements(t *testing.T) {
}

for i, test := range tests {
if got := ProductOfAllOtherElements(test.list); !reflect.DeepEqual(got, test.products) {
if got := ProductOfAllOtherElements(test.list); !slices.Equal(got, test.products) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.products, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions array/reverse_inplace_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand Down Expand Up @@ -31,7 +31,7 @@ func TestReverseInPlace(t *testing.T) {

for i, test := range tests {
ReverseInPlace(test.list, test.start, test.end)
if !reflect.DeepEqual(test.list, test.reversed) {
if !slices.Equal(test.list, test.reversed) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.reversed, test.list)
}
}
Expand Down
4 changes: 2 additions & 2 deletions array/rotate_k_steps_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package array

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestRotateKSteps(t *testing.T) {

for i, test := range tests {
RotateKSteps(test.list, test.steps)
if !reflect.DeepEqual(test.list, test.rotatedList) {
if !slices.Equal(test.list, test.rotatedList) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.rotatedList, test.list)
}
}
Expand Down
4 changes: 2 additions & 2 deletions backtracking/generate_parentheses_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package backtracking

import (
"reflect"
"slices"
"sort"
"testing"
)
Expand Down Expand Up @@ -32,7 +32,7 @@ func TestGenerateParentheses(t *testing.T) {
if len(got) > 0 {
sort.Strings(got)
}
if !reflect.DeepEqual(test.validParentheses, got) {
if !slices.Equal(test.validParentheses, got) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.validParentheses, got)
}
}
Expand Down
2 changes: 1 addition & 1 deletion backtracking/maze.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func mazeRecursive(x, y int, path string, wallMap map[[2]int]bool, visited [][]b
}

visited[x][y] = true
for i := 0; i < 4; i++ {
for i := range 4 {
nx, ny := x+directions[i][0], y+directions[i][1]
if nx >= 0 && nx < len(visited) && ny >= 0 && ny < len(visited[0]) && !visited[nx][ny] && !wallMap[[2]int{nx, ny}] {
if result := mazeRecursive(nx, ny, path+directionLetter[i], wallMap, visited, finish); result != "" {
Expand Down
3 changes: 1 addition & 2 deletions backtracking/maze_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package backtracking

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -39,7 +38,7 @@ func TestMaze(t *testing.T) {
}

for i, test := range tests {
if got := Maze(test.m, test.n, test.walls, test.start, test.finish); !reflect.DeepEqual(test.moves, got) {
if got := Maze(test.m, test.n, test.walls, test.start, test.finish); test.moves != got {
t.Fatalf("Failed test case #%d. Want %s got %s", i, test.moves, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions backtracking/n_queens.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func nQueensRecursive(row, n int, cols []int, output Chessboard) Chessboard {
output = append(output, append([]int{}, cols...))
return output
}
for col := 0; col < n; col++ {
for col := range n {
if isValidQueenPlacement(row, col, cols) {
cols[row] = col
output = nQueensRecursive(row+1, n, cols, output)
Expand All @@ -32,7 +32,7 @@ func nQueensRecursive(row, n int, cols []int, output Chessboard) Chessboard {
}

func isValidQueenPlacement(row, col int, cols []int) bool {
for i := 0; i < row; i++ {
for i := range row {
if col == cols[i] || col-row == cols[i]-i || col+row == cols[i]+i {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions backtracking/phone_letter_combinations_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package backtracking

import (
"reflect"
"slices"
"sort"
"testing"
)
Expand Down Expand Up @@ -33,7 +33,7 @@ func TestPhoneLetterCombinations(t *testing.T) {
if len(got) > 0 {
sort.Strings(got)
}
if !reflect.DeepEqual(test.combinations, got) {
if !slices.Equal(test.combinations, got) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.combinations, got)
}
}
Expand Down
4 changes: 2 additions & 2 deletions backtracking/sudoku.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package backtracking
// Sudoku solves the problem in O(9^(n*n)) time and O(n*n) space.
func Sudoku(board [][]int) bool {
for i := 0; i < len(board); i++ {
for j := 0; j < len(board[0]); j++ {
for j := range len(board[0]) {
if board[i][j] != 0 {
continue
}
Expand All @@ -24,7 +24,7 @@ func Sudoku(board [][]int) bool {
}

func isValidSudokuPlacement(board [][]int, row, col, value int) bool {
for i := 0; i < 9; i++ {
for i := range 9 {
if board[i][col] == value || board[row][i] == value || board[3*(row/3)+i/3][3*(col/3)+i%3] == value {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions dnc/merge_sort_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dnc

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestMergeSort(t *testing.T) {
}

for i, test := range tests {
if got := MergeSort(test.list); !reflect.DeepEqual(got, test.sorted) {
if got := MergeSort(test.list); !slices.Equal(got, test.sorted) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, got)
}
}
Expand Down
2 changes: 1 addition & 1 deletion dnc/quick_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func QuickSort(list []int) []int {
pivot := list[len(list)/2]

var less, equal, greater []int
for i := 0; i < len(list); i++ {
for i := range len(list) {
if list[i] == pivot {
equal = append(equal, list[i])
}
Expand Down
4 changes: 2 additions & 2 deletions dnc/quick_sort_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dnc

import (
"reflect"
"slices"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestQuickSort(t *testing.T) {
}

for i, test := range tests {
if got := QuickSort(test.list); !reflect.DeepEqual(got, test.sorted) {
if got := QuickSort(test.list); !slices.Equal(got, test.sorted) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.sorted, got)
}
}
Expand Down
8 changes: 4 additions & 4 deletions dnc/rate_limit_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dnc

import (
"reflect"
"slices"
"testing"
"time"
)
Expand Down Expand Up @@ -35,15 +35,15 @@ func TestRateLimiter(t *testing.T) {
for i, test := range tests {
rateLimitEvents = make([]int64, 0)
got := make([]bool, 0)
for i := 0; i < test.firstCallTimes; i++ {
for range test.firstCallTimes {
got = append(got, IsAllowed(test.limitPerSecond))
}
time.Sleep(time.Duration(test.sleep) * time.Second)
for i := 0; i < test.secondCallTimes; i++ {
for range test.secondCallTimes {
got = append(got, IsAllowed(test.limitPerSecond))
}

if !reflect.DeepEqual(got, test.want) {
if !slices.Equal(got, test.want) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.want, got)
}
}
Expand Down
2 changes: 1 addition & 1 deletion dnc/square_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func SquareRoot(number, precision int) float64 {
}

increment := 0.1
for i := 0; i < precision; i++ {
for range precision {
for ans*ans <= float64(number) {
ans += increment
}
Expand Down
4 changes: 2 additions & 2 deletions dnc/towers_of_hanoi_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dnc

import (
"reflect"
"slices"
"testing"
)

Expand Down Expand Up @@ -29,7 +29,7 @@ func TestTowerOfHanoi(t *testing.T) {
}

for i, test := range tests {
if got := TowerOfHanoi(test.n, test.start, test.end); !reflect.DeepEqual(got, test.moves) {
if got := TowerOfHanoi(test.n, test.start, test.end); !slices.Equal(got, test.moves) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.moves, got)
}
}
Expand Down
Loading

0 comments on commit a261c65

Please sign in to comment.