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