Skip to content

Instantly share code, notes, and snippets.

@frobs
Created January 17, 2023 11:23
Show Gist options
  • Save frobs/01321d04d3b163c02bc2d66ac44db334 to your computer and use it in GitHub Desktop.
Save frobs/01321d04d3b163c02bc2d66ac44db334 to your computer and use it in GitHub Desktop.
Testing file
defmodule MyTestScript do
@h 7
@fixed_value 37
def decode(num) do
decode_internal(num, "")
end
defp decode_internal(num, accum) when rem(num, @fixed_value) == 3 do
decode_internal(get_next_value(num , 3), accum <> "e")
end
defp decode_internal(num, accum) when rem(num, @fixed_value) == 1 do
decode_internal(get_next_value(num , 1), accum <> "c")
end
defp decode_internal(num, accum) when rem(num, @fixed_value) == 6 do
decode_internal(get_next_value(num , 6), accum <> "i")
end
defp decode_internal(num, accum) when rem(num, @fixed_value) == 9 do
decode_internal(get_next_value(num , 9), accum <> "n")
end
defp decode_internal(num, accum) when num == @h do
String.reverse(accum)
end
defp get_next_value(num, subtraction) do
trunc((num - subtraction) / @fixed_value)
end
defp decode_internal(_num, accum) do
String.reverse(accum)
end
end
#it could also be possible to reduce the letter-number relation to a map
#so that we do not have to rewrite the same logic so many times and it is the map
#that sets the conversion rules.
IO.puts(MyTestScript.decode(13583258))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment