Sha256: 22204cdcb2531a194c2e792768bce078653e75d2af24159c0b894b370da4d3ab

Contents?: true

Size: 1.16 KB

Versions: 69

Compression:

Stored size: 1.16 KB

Contents

defmodule AllYourBase do
  @doc """
  Given a number in base a, represented as a sequence of digits, converts it to base b,
  or returns nil if either of the bases are less than 2
  """

  @spec convert(list, integer, integer) :: list
  def convert(digits, base_a, base_b) do
    cond do
      base_a > 1 and base_b > 1 and digits != [] ->
        do_convert(digits, base_a, base_b)

      true ->
        nil
    end
  end

  defp do_convert(digits, base_a, base_b) do
    num = convert_to_num(digits, base_a, 0)

    case num do
      nil ->
        nil

      0 ->
        [0]

      num ->
        convert_to_digits(num, base_b, [])
        |> Enum.reverse()
    end
  end

  defp convert_to_num([head | tail], base_a, accumulator) do
    cond do
      head < base_a and head >= 0 ->
        convert_to_num(tail, base_a, accumulator * base_a + head)

      true ->
        nil
    end
  end

  defp convert_to_num([], _base_a, accumulator) do
    accumulator
  end

  defp convert_to_digits(num, base_b, arr) when num > 0 do
    convert_to_digits(div(num, base_b), base_b, arr ++ [rem(num, base_b)])
  end

  defp convert_to_digits(num, _base_b, arr) when num == 0 do
    arr
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/all-your-base/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/all-your-base/example.exs