Sha256: 0c1daedac1fa037d309f762721890496af3c94a677512973f200fcaae2647c66

Contents?: true

Size: 331 Bytes

Versions: 374

Compression:

Stored size: 331 Bytes

Contents

class Binary
  def self.to_decimal(binary)
    new(binary).to_decimal
  end

  def initialize(binary)
    raise ArgumentError.new if binary.match(/[^0-1]/)
    @binary = binary
  end

  def to_decimal
    binary.chars.reduce(0) do |decimal, char|
      (decimal * 2) + char.to_i
    end
  end

  private getter binary : String
end

Version data entries

374 entries across 374 versions & 1 rubygems

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