Exercism: Go version of the 'Triangle' exercise.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
// Package triangle determines what kind of triangle three side lengths make.
package triangle
import "math"
// An enumeration of triangle types
const ( NaT Kind = iota // Not a triangle
Equ // Equlateral
Iso // Isometric
Sca // Scalene
testVersion = 2 )
// A Kind is an integer representation of the kind of triangle.
type Kind int
// 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) (k 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 } } switch { // Return NaT if the sides fail triangle inequality tests.
case a+b < c || b+c < a || a+c < b: k = NaT case a == b && b == c: // 3 equal length sides (Equ)
k = Equ case a == b || b == c || a == c: // 2 equal length sides (Iso)
k = Iso case a != b && b != c && a != c: // No equal length sides (Sca)
k = Sca } return k }
// Iteration 2 benkmark results:
//
// PASS
// BenchmarkKind-12 2000000 651 ns/op
//
|