🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # freddie2025's solution

## to Triangle in the C# Track

Published at Apr 26 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

### TriangleTest.cs

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

using Xunit;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

public static class Triangle
{
public static bool IsScalene(double side1, double side2, double side3)
{
var sides = new HashSet<double>(){ side1, side2, side3 };
return sides.Count == 3 && !sides.Contains(0) && IsTriangle(side1, side2, side3);
}

public static bool IsIsosceles(double side1, double side2, double side3)
{
var sides = new HashSet<double>(){ side1, side2, side3 };
return sides.Count <= 2 && !sides.Contains(0) && IsTriangle(side1, side2, side3);
}

public static bool IsEquilateral(double side1, double side2, double side3)
{
var sides = new HashSet<double>(){ side1, side2, side3 };
return sides.Count == 1 && !sides.Contains(0) && IsTriangle(side1, side2, side3);
}

public static bool IsDegenerate(double side1, double side2, double side3)
{
double[] sides = { side1, side2, side3 };
var isDegenerate = (side1 + side2) == side3 || (side1 + side3) == side2 || (side2 + side3) == side1;
return isDegenerate && !sides.Contains(0) && IsTriangle(side1, side2, side3);
}

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