From fd484ec9cb380646a2b4017e64de2be16c04ed2a Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 25 Aug 2016 03:47:11 +0200 Subject: Triangle --- go/triangle/triangle.go | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'go/triangle/triangle.go') diff --git a/go/triangle/triangle.go b/go/triangle/triangle.go index 7243375..482cb60 100644 --- a/go/triangle/triangle.go +++ b/go/triangle/triangle.go @@ -1,17 +1,33 @@ package triangle -const testVersion = 2 +import "math" -// Code this function. -func KindFromSides(a, b, c float64) Kind +const testVersion = 2 -// Notice it returns this type. Pick something suitable. -type Kind +func KindFromSides(a, b, c float64) Kind { + invalid := func(n float64) bool { + return math.IsNaN(n) || math.IsInf(n, 0) || n <= 0 + } + if invalid(a) || invalid(b) || invalid(c) { + return NaT + } + if a+b < c || a+c < b || b+c < a { + return NaT + } + if a == b && b == c { + return Equ + } + if a == b || b == c || a == c { + return Iso + } + return Sca +} -// Pick values for the following identifiers used by the test program. -NaT // not a triangle -Equ // equilateral -Iso // isosceles -Sca // scalene +type Kind string -// Organize your code for readability. +const ( + NaT Kind = "not a triangle" + Equ Kind = "equilateral" + Iso Kind = "isosceles" + Sca Kind = "scalene" +) -- cgit v1.2.3