Sha256: 7b5bcf5652f320ff0929de1af5ca19042e1989cc59d89ce5072f135fd4b44fdd

Contents?: true

Size: 556 Bytes

Versions: 98

Compression:

Stored size: 556 Bytes

Contents

using System;
using System.Linq;

public class Binary
{
    public static int ToDecimal(string binary)
    {
        if (IsNotValidBinary(binary)) return 0;

        return binary
            .Select((c, i) => int.Parse(c.ToString()) * TwoToThePowerOf(binary.Length - i - 1))
            .Sum();
    }

    private static bool IsNotValidBinary(string binary)
    {
        return !binary.All(x => char.IsDigit(x) && int.Parse(x.ToString()) < 2);
    }

    private static int TwoToThePowerOf(int power)
    {
        return (int)Math.Pow(2, power);
    }
}

Version data entries

98 entries across 98 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.138 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.137 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.136 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.135 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.134 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.133 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.132 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.131 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.130 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.129 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.128 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.127 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.126 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.125 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.124 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.123 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.122 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.121 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.120 tracks/csharp/exercises/binary/Example.cs