# bugfry's solution

## to Triangle in the D Track

Published at Jul 13 2018 · 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.

## Getting Started

Make sure you have read D page on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

``````static if (all_tests_enabled)
``````

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard D facilities in preference to writing your own low-level algorithms or facilities by hand. DRefLanguage and DReference are references to the D language and D standard library.

## 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.d

``````module triangle;

unittest
{
import std.exception : assertThrown;

immutable int allTestsEnabled = 0;

// equilateral_triangles_have_equal_sides
{
assert(TriangleType.equilateral == kind(2, 2, 2));
}

static if (allTestsEnabled)
{
// larger_equilateral_triangles_also_have_equal_sides
{
assert(TriangleType.equilateral == kind(10, 10, 10));
}

// isosceles_triangles_have_last_two_sides_equal
{
assert(TriangleType.isosceles == kind(3, 4, 4));
}

// isosceles_triangles_have_first_and_last_sides_equal
{
assert(TriangleType.isosceles == kind(4, 3, 4));
}

// isosceles_triangles_have_first_two_sides_equal
{
assert(TriangleType.isosceles == kind(4, 4, 3));
}

// isosceles_triangles_have_in_fact_exactly_two_sides_equal
{
assert(TriangleType.isosceles == kind(10, 10, 2));
}

// scalene_triangles_have_no_equal_sides
{
assert(TriangleType.scalene == kind(3, 4, 5));
}

// scalene_triangles_have_no_equal_sides_at_a_larger_scale_too
{
assert(TriangleType.scalene == kind(10, 11, 12));
}

// scalene_triangles_have_no_equal_sides_in_descending_order_either
{
assert(TriangleType.scalene == kind(5, 4, 2));
}

// very_small_triangles_are_legal
{
assert(TriangleType.scalene == kind(0.4, 0.6, 0.3));
}

// triangles_with_no_size_are_illegal
{
assertThrown(kind(0, 0, 0));
}

// triangles_with_negative_sides_are_illegal
{
assertThrown(kind(3, 4, -5));
}

// triangles_violating_triangle_inequality_are_illegal
{
assertThrown(kind(1, 1, 3));
}

// larger_triangles_violating_triangle_inequality_are_illegal
{
assertThrown(kind(7, 3, 2));
}

}

}``````
``````module triangle;

import std.exception;
import std.algorithm : max, min;

enum TriangleType {
equilateral,
isosceles,
scalene
}

TriangleType kind(double a, double b, double c) {
if (min(a, b, c) <= 0) {
throw new Exception("Sides must be positive in length.");
}
if (2 * max(a, b, c) > a + b + c) {
throw new Exception("Sides don't join up.");
}

if (a == b && b == c) {
return TriangleType.equilateral;
} else if (a == b || b == c || c == a) {
return TriangleType.isosceles;
} else {
return TriangleType.scalene;
}
}

unittest {

immutable int allTestsEnabled = 1;

// equilateral_triangles_have_equal_sides
{
assert(TriangleType.equilateral == kind(2, 2, 2));
}

static if (allTestsEnabled)
{
// larger_equilateral_triangles_also_have_equal_sides
{
assert(TriangleType.equilateral == kind(10, 10, 10));
}

// isosceles_triangles_have_last_two_sides_equal
{
assert(TriangleType.isosceles == kind(3, 4, 4));
}

// isosceles_triangles_have_first_and_last_sides_equal
{
assert(TriangleType.isosceles == kind(4, 3, 4));
}

// isosceles_triangles_have_first_two_sides_equal
{
assert(TriangleType.isosceles == kind(4, 4, 3));
}

// isosceles_triangles_have_in_fact_exactly_two_sides_equal
{
assert(TriangleType.isosceles == kind(10, 10, 2));
}

// scalene_triangles_have_no_equal_sides
{
assert(TriangleType.scalene == kind(3, 4, 5));
}

// scalene_triangles_have_no_equal_sides_at_a_larger_scale_too
{
assert(TriangleType.scalene == kind(10, 11, 12));
}

// scalene_triangles_have_no_equal_sides_in_descending_order_either
{
assert(TriangleType.scalene == kind(5, 4, 2));
}

// very_small_triangles_are_legal
{
assert(TriangleType.scalene == kind(0.4, 0.6, 0.3));
}

// triangles_with_no_size_are_illegal
{
assertThrown(kind(0, 0, 0));
}

// triangles_with_negative_sides_are_illegal
{
assertThrown(kind(3, 4, -5));
}

// triangles_violating_triangle_inequality_are_illegal
{
assertThrown(kind(1, 1, 3));
}

// larger_triangles_violating_triangle_inequality_are_illegal
{
assertThrown(kind(7, 3, 2));
}

}

}

void main () {
}``````

### 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?