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.139 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.138 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.137 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.136 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.135 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.134 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.133 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.132 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.131 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.130 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.129 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.128 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.127 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.126 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.125 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.124 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.123 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.122 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.121 tracks/vbnet/exercises/binary/Example.vb
trackler-2.2.1.120 tracks/vbnet/exercises/binary/Example.vb