Sha256: 3c67165bfff6acf6f0b2208564c7e197da8296b7f7123efa690ea7412fe62d6d

Contents?: true

Size: 1.42 KB

Versions: 143

Compression:

Stored size: 1.42 KB

Contents

using System;
using System.Linq;

public enum TriangleKind
{
    Equilateral,
    Isosceles,
    Scalene
}

public static class Triangle
{
    public static TriangleKind Kind(decimal side1, decimal side2, decimal side3)
    {
        if (AllSidesAreZero(side1, side2, side3) ||
            HasImpossibleSides(side1, side2, side3) ||
            ViolatesTriangleInequality(side1, side2, side3))
        {
            throw new TriangleException();
        }

        int uniqueSides = UniqueSides(side1, side2, side3);
        if (uniqueSides == 1)
            return TriangleKind.Equilateral;
        if (uniqueSides == 2)
            return TriangleKind.Isosceles;
        return TriangleKind.Scalene;
    }

    private static bool AllSidesAreZero(decimal side1, decimal side2, decimal side3)
    {
        return side1 == 0 && side2 == 0 && side3 == 0;
    }

    private static bool HasImpossibleSides(decimal side1, decimal side2, decimal side3)
    {
        return side1 < 0 || side2 < 0 || side3 < 0;
    }

    private static bool ViolatesTriangleInequality(decimal side1, decimal side2, decimal side3)
    {
        return side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <= side1;
    }

    private static int UniqueSides(decimal side1, decimal side2, decimal side3)
    {
        decimal[] sides = { side1, side2, side3 };
        return sides.Distinct().Count();
    }
}

public class TriangleException : Exception { }

Version data entries

143 entries across 143 versions & 1 rubygems

Version Path
trackler-2.1.0.43 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.42 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.41 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.40 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.39 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.38 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.37 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.36 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.34 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.33 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.32 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.31 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.30 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.29 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.28 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.27 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.26 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.25 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.24 tracks/csharp/exercises/triangle/Example.cs
trackler-2.1.0.23 tracks/csharp/exercises/triangle/Example.cs