-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #576 from vaskoz/day282
Day282
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |