Skip to content

Latest commit

 

History

History
47 lines (35 loc) · 957 Bytes

038-bin2dec.livemd

File metadata and controls

47 lines (35 loc) · 957 Bytes

038 - Binario a Decimal

BINARIO A DECIMAL

Enunciado

Crea un programa se encargue de transformar un número binario a decimal sin utilizar funciones propias del lenguaje que lo hagan directamente.

Solución

defmodule Solution do
  def run(number) do
    to_string(number)
    |> String.graphemes()
    |> Enum.reverse()
    |> Enum.with_index()
    |> Enum.reduce(0, fn {item, index}, acc ->
      acc + String.to_integer(item) * Integer.pow(2, index)
    end)
  end
end
{:module, Solution, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:run, 1}}
Solution.run(11_010_111)
215