Skip to content

Instantly share code, notes, and snippets.

@imranismail
Created July 31, 2017 02:25
Show Gist options
  • Save imranismail/170d0082088201cdc29e661470c79439 to your computer and use it in GitHub Desktop.
Save imranismail/170d0082088201cdc29e661470c79439 to your computer and use it in GitHub Desktop.
BinaryGap in Elixir
defmodule BinaryGap do
def max(number) when is_number(number) do
number
|> Integer.to_string(2)
|> calculate()
end
defp calculate(binary, count \\ -1, max \\ 0)
defp calculate("1" <> remaining, count, max) do
calculate(remaining, 0, Enum.max([max, count]))
end
defp calculate("0" <> remaining, count, max) when count >= 0 do
calculate(remaining, count + 1, max)
end
defp calculate("0" <> remaining, count, max) when count < 0 do
calculate(remaining, count, max)
end
defp calculate("", _count, max) do
max
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment