Sha256: 2f08481ce24e718368389df0b7aed56eab09c54194b4506221fb569ec6bc26c2

Contents?: true

Size: 705 Bytes

Versions: 396

Compression:

Stored size: 705 Bytes

Contents

Public Class Binary
    Private ReadOnly value As String

    Public Sub New(value As String)
        Me.value = value
    End Sub

    Public Function ToDecimal() As Integer
        If IsNotValidBinary() Then
            Return 0
        End If

        Return value.Select(Function(c, i) Integer.Parse(c.ToString()) * TwoToThePowerOf(value.Length - i - 1)).Sum()
    End Function

    Private Function IsNotValidBinary() As Boolean
        Return Not value.All(Function(x) Char.IsDigit(x) AndAlso Integer.Parse(x.ToString()) < 2)
    End Function

    Private Shared Function TwoToThePowerOf(power As Integer) As Integer
        Return CInt(Math.Truncate(Math.Pow(2, power)))
    End Function
End Class

Version data entries

396 entries across 396 versions & 1 rubygems

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