Sha256: c3ac32015148cea603c59dcf7fe892e0b246305842e8a3aa0f9f1fbf93008b87

Contents?: true

Size: 1.91 KB

Versions: 20

Compression:

Stored size: 1.91 KB

Contents

module triangle;

import std.exception;

enum TriangleType { equilateral, isosceles, scalene };

/**
 * Return what kind of triagle that is.
 */
TriangleType kind (double a, double b, double c)
{
	// check the triangle inequality
	if ( a + b <= c || a + c <= b || c + b <= a)
	{
		throw new Exception("Segment lengths do not satisfy triangle inequality.");
	}

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

unittest
{

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

// 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));
}

}

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
trackler-2.1.0.48 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.47 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.46 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.45 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.44 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.43 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.42 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.41 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.40 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.39 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.38 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.37 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.36 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.34 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.33 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.32 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.31 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.30 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.29 tracks/dlang/exercises/triangle/example/triangle.d
trackler-2.1.0.28 tracks/dlang/exercises/triangle/example/triangle.d