Skip to content

Commit

Permalink
Merge pull request #576 from vaskoz/day282
Browse files Browse the repository at this point in the history
Day282
  • Loading branch information
vaskoz committed Jun 1, 2019
2 parents 946173a + 1ec8ca1 commit 35e9df0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,4 @@ problems from
* [Day 279](https://github.com/vaskoz/dailycodingproblem-go/issues/566)
* [Day 280](https://github.com/vaskoz/dailycodingproblem-go/issues/568)
* [Day 281](https://github.com/vaskoz/dailycodingproblem-go/issues/572)
* [Day 282](https://github.com/vaskoz/dailycodingproblem-go/issues/575)
58 changes: 58 additions & 0 deletions day282/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package day282

import (
"errors"
"math"
)

var errNoAnswer = errors.New("no pythagorean triplet exists")

// ErrNoAnswer is the error returned if a solution doesn't exist.
func ErrNoAnswer() error {
return errNoAnswer
}

// PythagoreanTripletBrute returns the triplets that fulfill
// a^2+b^2=c^2.
// Runs in O(N^3) time and O(1) space.
func PythagoreanTripletBrute(nums []int) (a, b, c int, err error) {
for i := range nums {
for j := range nums {
for k := range nums {
if i == j || j == k || i == k {
continue
}
if nums[i]*nums[i]+nums[j]*nums[j] == nums[k]*nums[k] {
if nums[i] < nums[j] {
return nums[i], nums[j], nums[k], nil
}
return nums[j], nums[i], nums[k], nil
}
}
}
}
return 0, 0, 0, errNoAnswer
}

// PythagoreanTripletBrute returns the triplets that fulfill
// a^2+b^2=c^2.
// Runs in O(N^2) time and O(N) space.
func PythagoreanTriplet(nums []int) (a, b, c int, err error) {
squared := make(map[int]struct{}, len(nums))
for _, num := range nums {
squared[num*num] = struct{}{}
}
for i := range nums {
for j := range nums {
total := nums[i]*nums[i] + nums[j]*nums[j]
c := int(math.Sqrt(float64(total)))
if _, found := squared[total]; found {
if nums[i] < nums[j] {
return nums[i], nums[j], c, nil
}
return nums[j], nums[i], c, nil
}
}
}
return 0, 0, 0, errNoAnswer
}
60 changes: 60 additions & 0 deletions day282/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package day282

import "testing"

// nolint
var testcases = []struct {
nums []int
a, b, c int
err error
}{
{
[]int{10, 11, 12},
0, 0, 0,
ErrNoAnswer(),
},
{
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9},
3, 4, 5,
nil,
},
{
[]int{9, 8, 7, 6, 5, 4, 3, 2, 1},
3, 4, 5,
nil,
},
}

func TestPythagoreanTriplet(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if a, b, c, err := PythagoreanTriplet(tc.nums); a != tc.a || b != tc.b || c != tc.c || err != tc.err {
t.Errorf("Expected (%v,%v,%v,%v), got (%v,%v,%v,%v)", tc.a, tc.b, tc.c, tc.err, a, b, c, err)
}
}
}

func BenchmarkPythagoreanTriplet(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
PythagoreanTriplet(tc.nums) // nolint
}
}
}

func TestPythagoreanTripletBrute(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if a, b, c, err := PythagoreanTripletBrute(tc.nums); a != tc.a || b != tc.b || c != tc.c || err != tc.err {
t.Errorf("Expected (%v,%v,%v,%v), got (%v,%v,%v,%v)", tc.a, tc.b, tc.c, tc.err, a, b, c, err)
}
}
}

func BenchmarkPythagoreanTripletBrute(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
PythagoreanTripletBrute(tc.nums) // nolint
}
}
}

0 comments on commit 35e9df0

Please sign in to comment.