Created
July 31, 2017 02:25
-
-
Save imranismail/170d0082088201cdc29e661470c79439 to your computer and use it in GitHub Desktop.
BinaryGap in Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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