Skip to content

Commit 277abf2

Browse files
committed
add ci; clean up
1 parent 62075cf commit 277abf2

File tree

12 files changed

+370
-284
lines changed

12 files changed

+370
-284
lines changed

.golangci.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
linters:
2+
enable-all: true
3+
disable:
4+
- gochecknoglobals
5+
6+
linters-settings:
7+
errcheck:
8+
9+
gofmt:
10+
simplify: true
11+
govet:
12+
check-shadowing: true
13+
golint:
14+
min-confidence: 0
15+
gocyclo:
16+
min-complexity: 10
17+
maligned:
18+
suggest-new: true
19+
dupl:
20+
threshold: 100
21+
goconst:
22+
min-len: 2
23+
min-occurrences: 2
24+
misspell:
25+
locale: US
26+
lll:
27+
line-length: 120
28+
goimports:
29+
local-prefixes: github.com/oleg-balunenko/leetcode_problems/
30+
unparam:
31+
algo: cha
32+
check-exported: false
33+
prealloc:
34+
simple: true
35+
range-loops: true # Report preallocation suggestions on range loops, true by default
36+
for-loops: false # Report preallocation suggestions on for loops, false by default
37+
gocritic:
38+
enabled-checks:
39+
- docStub
40+
- rangeValCopy
41+
- yodaStyleExpr
42+
- appendAssign
43+
- appendCombine
44+
- caseOrder
45+
- badCond
46+
- commentedOutCode
47+
- commentFormatting
48+
- commentedOutImport
49+
- dupArg
50+
- dupBranchBody
51+
- elseif
52+
- emptyStringTest
53+
- indexAlloc
54+
- initClause
55+
- captlocal
56+
- weakCond
57+
- deprecatedComment
58+
- flagDeref
59+
- flagName
60+
- hugeParam
61+
- ifElseChain
62+
- nilValReturn
63+
- rangeExprCopy
64+
- ptrToRefParam
65+
- underef
66+
- unnecessaryBlock
67+
- valSwap
68+
settings: # settings passed to gocritic
69+
captLocal: # must be valid enabled check name
70+
paramsOnly: true
71+
rangeValCopy:
72+
sizeThreshold: 320
73+
hugeParam:
74+
sizeThreshold: 500
75+
rangeExprCopy:
76+
skipTestFuncs: true
77+
underef:
78+
skipRecvDeref: true
79+
80+
81+
run:
82+
83+
84+
skip-dirs:
85+
- vendor/
86+
87+
output:
88+
format: colored-line-number
89+

.travis.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
language: go
2+
dist: trusty
3+
sudo: false
4+
install: true
5+
env:
6+
global:
7+
- MYAPP=leetcode_problems
8+
- MYEMAIL=oleg.balunenko@gmail.com
9+
- GOPROXY=https://gocenter.io
10+
11+
jobs:
12+
include:
13+
- stage: Unit Tests, Vet
14+
go:
15+
- 1.13.x
16+
os:
17+
- linux
18+
install:
19+
- go mod tidy -v
20+
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin
21+
22+
23+
script:
24+
- echo "Run unit tests..."
25+
- go vet $(go list ./...)
26+
- go test -v -race $(go list ./...)
27+
- golangci-lint run

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/oleg-balunenko/leetcode_problems
2+
3+
go 1.12
4+
5+
require github.com/stretchr/testify v1.4.0

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
7+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
11+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

hamming_distance/hamming_test.go

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,58 @@
1-
package hamming_distance
1+
package main
22

33
import (
44
"fmt"
55
"testing"
6-
)
7-
8-
type TestCase struct {
9-
id int
10-
description string
11-
input Input
12-
expectedResult int
13-
}
146

15-
type Input struct {
16-
a int
17-
b int
18-
}
7+
"github.com/stretchr/testify/assert"
8+
)
199

20-
type TestSuite []TestCase
10+
func TestHamming(t *testing.T) {
11+
type args struct {
12+
a int
13+
b int
14+
}
2115

22-
var ts = TestSuite{
23-
TestCase{
24-
id: 1,
25-
description: "test case from task - different numbers",
26-
input: Input{
27-
a: 1,
28-
b: 4,
16+
var tests = []struct {
17+
id int
18+
description string
19+
input args
20+
expectedResult int
21+
}{
22+
{
23+
id: 1,
24+
description: "test case from task - different numbers",
25+
input: args{
26+
a: 1,
27+
b: 4,
28+
},
29+
expectedResult: 2,
2930
},
30-
expectedResult: 2,
31-
},
32-
TestCase{
33-
id: 2,
34-
description: "Diggerent length of binary",
35-
input: Input{
36-
a: 1,
37-
b: 136,
31+
{
32+
id: 2,
33+
description: "Diggerent length of binary",
34+
input: args{
35+
a: 1,
36+
b: 136,
37+
},
38+
expectedResult: 3,
3839
},
39-
expectedResult: 3,
40-
},
41-
TestCase{
42-
id: 3,
43-
description: "Same numbers",
44-
input: Input{
45-
a: 136,
46-
b: 136,
40+
{
41+
id: 3,
42+
description: "Same numbers",
43+
input: args{
44+
a: 136,
45+
b: 136,
46+
},
47+
expectedResult: 0,
4748
},
48-
expectedResult: 0,
49-
},
50-
}
51-
52-
func TestHamming(t *testing.T) {
49+
}
5350

54-
for _, tc := range ts {
51+
for _, tc := range tests {
52+
tc := tc
5553
t.Run(fmt.Sprintf("Test%d", tc.id), func(t *testing.T) {
56-
5754
actualResult := hammingDistance(tc.input.a, tc.input.b)
58-
59-
if actualResult != tc.expectedResult {
60-
fmt.Printf(" - Got: %d\n - Expected: %d\n", actualResult, tc.expectedResult)
61-
t.Fail()
62-
}
63-
55+
assert.Equal(t, tc.expectedResult, actualResult)
6456
})
6557
}
66-
6758
}

hamming_distance/main.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,40 @@
1-
package hamming_distance
1+
package main
22

33
import (
4-
"fmt"
54
"strconv"
65
)
76

87
func hammingDistance(x int, y int) int {
9-
var result int
8+
var (
9+
result int
10+
diffLen int
11+
)
12+
1013
binX := strconv.FormatInt(int64(x), 2)
1114
binY := strconv.FormatInt(int64(y), 2)
1215

13-
fmt.Printf("Before diff fix:\n - X: %s\n - Y: %s\n", binX, binY)
14-
var diffLen int
15-
16-
switch {
17-
case len(binX) < len(binY):
16+
if len(binX) < len(binY) {
1817
diffLen = len(binY) - len(binX)
1918
binX = addLeadZeros(binX, diffLen)
20-
21-
case len(binX) > len(binY):
19+
} else {
2220
diffLen = len(binX) - len(binY)
2321
binY = addLeadZeros(binY, diffLen)
2422
}
2523

26-
fmt.Printf("After diff fix\n - X: %s\n - Y: %s\n", binX, binY)
2724
for index, digit := range binX {
2825
if string([]rune(binY)[index]) != string(digit) {
29-
3026
result++
31-
3227
}
3328
}
3429

3530
return result
3631
}
3732

3833
func addLeadZeros(bin string, diff int) string {
39-
//fmt.Printf("Diff is %d\n", diff)
40-
//fmt.Printf("string is %s\n", bin)
41-
4234
var tmp string
4335
for i := 0; i < diff; i++ {
4436
tmp += "0"
45-
//fmt.Printf("Step[%d]: tmp is [%s]\n", i, tmp)
4637
}
47-
bin = tmp + bin
48-
return bin
38+
39+
return tmp + bin
4940
}

0 commit comments

Comments
 (0)