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.180 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.179 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.178 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.177 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.176 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.175 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.174 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.173 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.172 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.171 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.170 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.169 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.167 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.166 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.165 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.164 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.163 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.162 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.161 tracks/csharp/exercises/binary/Example.cs
trackler-2.2.1.160 tracks/csharp/exercises/binary/Example.cs