Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 282
Good morning! Here's your coding interview problem for today.

This problem was asked by Netflix.

Given an array of integers, determine whether it contains a Pythagorean triplet. Recall that a Pythogorean triplet `(a, b, c)` is defined by the equation `a^2+ b^2= c^2`.

Pull request description:
Day282
Resolves #575.

Patch:
diff --git a/README.md b/README.md
index 57cc65e..b8df42c 100644
--- a/README.md
+++ b/README.md
@@ -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)
diff --git a/day282/problem.go b/day282/problem.go
new file mode 100644
index 0000000..0a80e6e
--- /dev/null
+++ b/day282/problem.go
@@ -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
+}


Test patch:
diff --git a/day282/problem_test.go b/day282/problem_test.go
new file mode 100644
index 0000000..2597729
--- /dev/null
+++ b/day282/problem_test.go
@@ -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
+		}
+	}
+}