|
|
@ -1,17 +1,47 @@ |
|
|
|
package triangle |
|
|
|
|
|
|
|
const testVersion = 2 |
|
|
|
import "math" |
|
|
|
|
|
|
|
// Code this function.
|
|
|
|
func KindFromSides(a, b, c float64) Kind |
|
|
|
const ( |
|
|
|
// An enumeration of triangle types
|
|
|
|
NaT Kind = iota |
|
|
|
Equ |
|
|
|
Iso |
|
|
|
Sca |
|
|
|
|
|
|
|
// Notice it returns this type. Pick something suitable.
|
|
|
|
type Kind |
|
|
|
testVersion = 2 |
|
|
|
) |
|
|
|
|
|
|
|
// Pick values for the following identifiers used by the test program.
|
|
|
|
NaT // not a triangle
|
|
|
|
Equ // equilateral
|
|
|
|
Iso // isosceles
|
|
|
|
Sca // scalene
|
|
|
|
// A Kind is an integer representation of the kind of triangle.
|
|
|
|
type Kind int |
|
|
|
|
|
|
|
// Organize your code for readability.
|
|
|
|
// KindFromSides determines whether the three side lengths given
|
|
|
|
// constitute a triangle and if so, what type of triangle it is.
|
|
|
|
func KindFromSides(a, b, c float64) Kind { |
|
|
|
// Return NaT when any side is zero, negative, infinite, or NaN.
|
|
|
|
for _, f := range []float64{a, b, c} { |
|
|
|
if f <= 0 || f != f || math.IsInf(f, 0) { |
|
|
|
return NaT |
|
|
|
} |
|
|
|
} |
|
|
|
// Return NaT if the sides fail triangle inequality tests.
|
|
|
|
if a+b < c || b+c < a || a+c < b { |
|
|
|
return NaT |
|
|
|
} |
|
|
|
if a == b && b == c { |
|
|
|
return Equ |
|
|
|
} |
|
|
|
if a == b || b == c || a == c { |
|
|
|
return Iso |
|
|
|
} |
|
|
|
if a != b || b != c || a != c { |
|
|
|
return Sca |
|
|
|
} |
|
|
|
return NaT |
|
|
|
} |
|
|
|
|
|
|
|
// Iteration 1 benkmark results:
|
|
|
|
//
|
|
|
|
// PASS
|
|
|
|
// BenchmarkKind-12 2000000 643 ns/op
|
|
|
|
// ok exercism/go/triangle 1.948s
|