Sha256: 1ab74cf38b32a828dad48a0f0c1fe860bebe3c77e654784f63601969d9a04c3c

Contents?: true

Size: 1.41 KB

Versions: 115

Compression:

Stored size: 1.41 KB

Contents

using System;
using System.Linq;

public enum TriangleKind
{
    Equilateral,
    Isosceles,
    Scalene
}

public 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

115 entries across 115 versions & 1 rubygems

Version Path
trackler-2.0.8.14 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.13 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.12 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.11 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.10 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.9 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.8 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.7 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.6 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.5 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.4 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.3 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.2 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.8.1 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.7.0 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.6.44 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.6.43 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.6.42 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.6.41 tracks/csharp/exercises/triangle/Example.cs
trackler-2.0.6.40 tracks/csharp/exercises/triangle/Example.cs