Avatar of artemkorsakov

artemkorsakov's solution

to Triangle in the Go Track

Published at Feb 12 2019 · 0 comments
Instructions
Test suite
Solution

Determine if a triangle is equilateral, isosceles, or scalene.

An equilateral triangle has all three sides the same length.

An isosceles triangle has at least two sides the same length. (It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A scalene triangle has all sides of different lengths.

Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. See Triangle Inequality.

Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

Constant Declarations

In this exercise you are asked to declare a series of constants used to identify different types of triangles. Pick the data type you think works best for this and get the tests passing.

Afterwards, it may be worth reading about Go's predeclared identifier iota. There's an excellent write up about it here.

Running the tests

To run the tests run the command go test from within the exercise directory.

If the test suite contains benchmarks, you can run these with the --bench and --benchmem flags:

go test -v --bench . --benchmem

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

Further information

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language page.

Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

triangle_test.go

package triangle

import (
	"math"
	"testing"
)

type testCase struct {
	want    Kind
	a, b, c float64
}

// basic test cases
var testData = []testCase{
	{Equ, 2, 2, 2},    // same length
	{Equ, 10, 10, 10}, // a little bigger
	{Iso, 3, 4, 4},    // last two sides equal
	{Iso, 4, 3, 4},    // first and last sides equal
	{Iso, 4, 4, 3},    // first two sides equal
	{Iso, 10, 10, 2},  // again
	{Iso, 2, 4, 2},    // a "triangle" that is just a line is still OK
	{Sca, 3, 4, 5},    // no sides equal
	{Sca, 10, 11, 12}, // again
	{Sca, 5, 4, 2},    // descending order
	{Sca, .4, .6, .3}, // small sides
	{Sca, 1, 4, 3},    // a "triangle" that is just a line is still OK
	{Sca, 5, 4, 6},    // 2a == b+c looks like equilateral, but isn't always.
	{Sca, 6, 4, 5},    // 2a == b+c looks like equilateral, but isn't always.
	{NaT, 0, 0, 0},    // zero length
	{NaT, 3, 4, -5},   // negative length
	{NaT, 1, 1, 3},    // fails triangle inequality
	{NaT, 2, 5, 2},    // another
	{NaT, 7, 3, 2},    // another
}

// generate cases with NaN and Infs, append to basic cases
func init() {
	nan := math.NaN()
	pinf := math.Inf(1)
	ninf := math.Inf(-1)
	nf := make([]testCase, 4*4*4)
	i := 0
	for _, a := range []float64{3, nan, pinf, ninf} {
		for _, b := range []float64{4, nan, pinf, ninf} {
			for _, c := range []float64{5, nan, pinf, ninf} {
				nf[i] = testCase{NaT, a, b, c}
				i++
			}
		}
	}
	testData = append(testData, nf[1:]...)
}

// Test that the kinds are not equal to each other.
// If they are equal, then TestKind will return false positives.
func TestKindsNotEqual(t *testing.T) {
	kindsAndNames := []struct {
		kind Kind
		name string
	}{
		{Equ, "Equ"},
		{Iso, "Iso"},
		{Sca, "Sca"},
		{NaT, "NaT"},
	}

	for i, pair1 := range kindsAndNames {
		for j := i + 1; j < len(kindsAndNames); j++ {
			pair2 := kindsAndNames[j]
			if pair1.kind == pair2.kind {
				t.Fatalf("%s should not be equal to %s", pair1.name, pair2.name)
			}
		}
	}
}

func TestKind(t *testing.T) {
	for _, test := range testData {
		got := KindFromSides(test.a, test.b, test.c)
		if got != test.want {
			t.Fatalf("Triangle with sides, %g, %g, %g = %v, want %v",
				test.a, test.b, test.c, got, test.want)
		}
	}
}

func BenchmarkKind(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range testData {
			KindFromSides(test.a, test.b, test.c)
		}
	}
}
/*
Package triangle determines the type of triangle given by the length of the sides.
 */
package triangle

import (
	"math"
)

// Kind is triangle type
type Kind int

const (
	// NaT is not a triangle
	NaT = iota
	// Equ is an equilateral triangle
	Equ
	// Iso is an isosceles triangle
	Iso
	// Sca is a scalene triangle
	Sca
)

const float64EqualityThreshold = 1e-9

// KindFromSides returns triangle type by specified side lengths.
func KindFromSides(a, b, c float64) Kind {
	if IsNat(a, b, c) {
		return NaT
	}
	if IsEqu(a, b, c) {
		return Equ
	}
	if IsIso(a, b, c) {
		return Iso
	}

	return Sca
}

// IsNat returns whether a triangle is a set of sides.
func IsNat(a, b, c float64) bool {
	return a <= 0 || b <= 0 || c <= 0 ||
		math.IsNaN(a) || math.IsNaN(b) || math.IsNaN(c) ||
		math.IsInf(a, 0) || math.IsInf(b, 0) || math.IsInf(c, 0) ||
		a+b < c || b+c < a || a+c < b
}

// IsEqu returns whether a equilateral triangle is a set of sides.
func IsEqu(a, b, c float64) bool {
	return math.Abs(a-b) <= float64EqualityThreshold && math.Abs(a-c) <= float64EqualityThreshold
}

// IsIso returns whether a isosceles triangle is a set of sides.
func IsIso(a, b, c float64) bool {
	return math.Abs(a-b) <= float64EqualityThreshold ||
		math.Abs(c-b) <= float64EqualityThreshold ||
		math.Abs(a-c) <= float64EqualityThreshold
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?