 artemkorsakov's solution

to Triangle in the C# Track

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

Note:

This exercise has changed since this solution was written.

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.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit Triangle.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# 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.

TriangleTest.cs

// This file was auto-generated based on version 1.2.0 of the canonical data.

using Xunit;

public class TriangleTest
{
[Fact]
public void Returns_true_if_the_triangle_is_equilateral_true_if_all_sides_are_equal()
{
Assert.True(Triangle.IsEquilateral(2, 2, 2));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_equilateral_false_if_any_side_is_unequal()
{
Assert.False(Triangle.IsEquilateral(2, 3, 2));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_equilateral_false_if_no_sides_are_equal()
{
Assert.False(Triangle.IsEquilateral(5, 4, 6));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_equilateral_all_zero_sides_are_illegal_so_the_triangle_is_not_equilateral()
{
Assert.False(Triangle.IsEquilateral(0, 0, 0));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_equilateral_sides_may_be_floats()
{
Assert.True(Triangle.IsEquilateral(0.5, 0.5, 0.5));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_isosceles_true_if_last_two_sides_are_equal()
{
Assert.True(Triangle.IsIsosceles(3, 4, 4));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_isosceles_true_if_first_two_sides_are_equal()
{
Assert.True(Triangle.IsIsosceles(4, 4, 3));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_isosceles_true_if_first_and_last_sides_are_equal()
{
Assert.True(Triangle.IsIsosceles(4, 3, 4));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_isosceles_equilateral_triangles_are_also_isosceles()
{
Assert.True(Triangle.IsIsosceles(4, 4, 4));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_isosceles_false_if_no_sides_are_equal()
{
Assert.False(Triangle.IsIsosceles(2, 3, 4));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_isosceles_sides_that_violate_triangle_inequality_are_not_isosceles_even_if_two_are_equal_1_()
{
Assert.False(Triangle.IsIsosceles(1, 1, 3));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_isosceles_sides_that_violate_triangle_inequality_are_not_isosceles_even_if_two_are_equal_2_()
{
Assert.False(Triangle.IsIsosceles(1, 3, 1));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_isosceles_sides_that_violate_triangle_inequality_are_not_isosceles_even_if_two_are_equal_3_()
{
Assert.False(Triangle.IsIsosceles(3, 1, 1));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_isosceles_sides_may_be_floats()
{
Assert.True(Triangle.IsIsosceles(0.5, 0.4, 0.5));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_scalene_true_if_no_sides_are_equal()
{
Assert.True(Triangle.IsScalene(5, 4, 6));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_scalene_false_if_all_sides_are_equal()
{
Assert.False(Triangle.IsScalene(4, 4, 4));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_scalene_false_if_two_sides_are_equal()
{
Assert.False(Triangle.IsScalene(4, 4, 3));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_scalene_sides_that_violate_triangle_inequality_are_not_scalene_even_if_they_are_all_different()
{
Assert.False(Triangle.IsScalene(7, 3, 2));
}

[Fact(Skip = "Remove to run test")]
public void Returns_true_if_the_triangle_is_scalene_sides_may_be_floats()
{
Assert.True(Triangle.IsScalene(0.5, 0.4, 0.6));
}
}
using System;

public static class Triangle
{
public static bool IsScalene(double side1, double side2, double side3)
{
return IsTriangle(side1, side2, side3) &&
Math.Abs(side1 - side2) > double.Epsilon &&
Math.Abs(side1 - side3) > double.Epsilon &&
Math.Abs(side2 - side3) > double.Epsilon;
}

public static bool IsIsosceles(double side1, double side2, double side3)
{
return IsTriangle(side1, side2, side3) &&
(Math.Abs(side1 - side2) < double.Epsilon ||
Math.Abs(side3 - side2) < double.Epsilon ||
Math.Abs(side3 - side1) < double.Epsilon);
}

public static bool IsEquilateral(double side1, double side2, double side3)
{
return IsTriangle(side1, side2, side3) &&
Math.Abs(side1 - side2) < double.Epsilon &&
Math.Abs(side3 - side2) < double.Epsilon;
}

private static bool IsTriangle(double side1, double side2, double side3)
{
return side1 > 0 && side2 > 0 && side3 > 0 &&
side1 + side2 >= side3 &&
side1 + side3 >= side2 &&
side3 + side2 >= side1;
}
}

public class TriangleException : Exception { }