Current section
Files
Jump to
Current section
Files
lib/guesswork/constraint/arithmetic/internal_variable/base_36.ex
defmodule Guesswork.Constraint.Arithmetic.InternalVariable.Base36 do
@moduledoc """
A simple module for building the base36 string version of an integer.
"""
defp get_char(x) do
if x < 26, do: x + 65, else: x + 48 - 25
end
defp get_char_list(x) when x < 0, do: []
defp get_char_list(x), do: [get_char(Integer.mod(x, 36)) | get_char_list(div(x, 36) - 1)]
@doc """
Returns the base 36 string of the the integer prefixed with `_`.
## Examples
iex> Guesswork.Constraint.Arithmetic.InternalVariable.Base36.calculate(0)
"_A"
iex> Guesswork.Constraint.Arithmetic.InternalVariable.Base36.calculate(15)
"_P"
iex> Guesswork.Constraint.Arithmetic.InternalVariable.Base36.calculate(27)
"_2"
iex> Guesswork.Constraint.Arithmetic.InternalVariable.Base36.calculate(51)
"_AP"
iex> Guesswork.Constraint.Arithmetic.InternalVariable.Base36.calculate(1118)
"_5C"
"""
@spec calculate(integer()) :: String.t()
def calculate(x) do
"_#{x |> get_char_list() |> Enum.reverse() |> to_string()}"
end
end