Sha256: 02684bfc12844999e517740638b5733c7f7cd051d4d123c21315e753995cd90c

Contents?: true

Size: 1.95 KB

Versions: 138

Compression:

Stored size: 1.95 KB

Contents

using System;
using System.Linq;

public enum TriangleKind
{
    Equilateral,
    Isosceles,
    Scalene,
    Invalid
}

public static class Triangle
{
    
    public static bool IsScalene(double side1, double side2, double side3)
    {
        return Kind(side1, side2, side3) == TriangleKind.Scalene;
    }

    public static bool IsIsosceles(double side1, double side2, double side3) 
    {
        var triangle = Kind(side1, side2, side3);
        return triangle == TriangleKind.Isosceles || triangle == TriangleKind.Equilateral;
    }

    public static bool IsEquilateral(double side1, double side2, double side3) 
    {
        return Kind(side1, side2, side3) == TriangleKind.Equilateral;
    }

    private static TriangleKind Kind(double side1, double side2, double side3)
    {
        if (AllSidesAreZero(side1, side2, side3) ||
            HasImpossibleSides(side1, side2, side3) ||
            ViolatesTriangleInequality(side1, side2, side3))
        {
            return TriangleKind.Invalid;
        }

        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(double side1, double side2, double side3)
    {
        return side1 == 0 && side2 == 0 && side3 == 0;
    }

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

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

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

public class TriangleException : Exception { }

Version data entries

138 entries across 138 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.179 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.178 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.177 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.176 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.175 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.174 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.173 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.172 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.171 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.170 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.169 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.167 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.166 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.165 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.164 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.163 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.162 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.161 tracks/csharp/exercises/triangle/Example.cs
trackler-2.2.1.160 tracks/csharp/exercises/triangle/Example.cs