Skip to content

Commit da160d4

Browse files
committed
Solve 'Find The Parity Outlier' kata
1 parent f08edc1 commit da160d4

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

find_the_parity_outlier/kata.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package kata
2+
3+
// FindOutlier https://www.codewars.com/kata/5526fc09a1bbd946250002dc/train/go
4+
func FindOutlier(integers []int) int {
5+
var even, odd *int
6+
var oddCount, evenCount uint
7+
8+
for _, i := range integers {
9+
value := i // See https://github.com/kyoh86/exportloopref
10+
11+
if i%2 == 0 {
12+
evenCount++
13+
even = &value
14+
} else {
15+
oddCount++
16+
odd = &value
17+
}
18+
19+
if evenCount > 1 && odd != nil {
20+
return *odd
21+
}
22+
23+
if oddCount > 1 && even != nil {
24+
return *even
25+
}
26+
}
27+
28+
panic("no outlier found")
29+
}

find_the_parity_outlier/kata_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package kata_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
. "go-katas/find_the_parity_outlier"
7+
"math"
8+
"testing"
9+
)
10+
11+
var _ = Describe("Example test", func() {
12+
It("Example test case", func() {
13+
Expect(FindOutlier([]int{2, 6, 8, -10, 3})).To(Equal(3))
14+
Expect(FindOutlier([]int{206847684, 1056521, 7, 17, 1901, 21104421, 7, 1, 35521, 1, 7781})).To(Equal(206847684))
15+
Expect(FindOutlier([]int{math.MaxInt32, 0, 1})).To(Equal(0))
16+
})
17+
18+
It("Odd in the front, positive", func() {
19+
Expect(FindOutlier([]int{3, 6, 8, -10})).To(Equal(3))
20+
})
21+
22+
It("Odd in the front, negative", func() {
23+
Expect(FindOutlier([]int{-3, 6, 8, 10})).To(Equal(-3))
24+
})
25+
})
26+
27+
func TestSuite(t *testing.T) {
28+
RegisterFailHandler(Fail)
29+
RunSpecs(t, "Find The Parity Outlier")
30+
}

0 commit comments

Comments
 (0)