Last active
August 29, 2015 14:20
-
-
Save pmarreck/9bff88bfbd27e7b552f1 to your computer and use it in GitHub Desktop.
Last question on the "Five programming problems every software engineer should be able to solve in < 1 hour" from Reddit, 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 Upto100 do | |
def go do | |
explore("1", 1, {1, []}) | |
end | |
# spillover cases | |
def explore(_, current_num, _) when current_num > 9 do | |
end | |
# exactly 100 case | |
def explore(concatenated_string, 9, {100,[]}) do | |
IO.puts concatenated_string | |
end | |
# all other cases | |
def explore(concatenated_string, current_num, _) do | |
explore(concatenated_string <> to_string(current_num+1), current_num+1, Code.eval_string(concatenated_string <> to_string(current_num+1))) | |
explore(concatenated_string <> "+" <> to_string(current_num+1), current_num+1, Code.eval_string(concatenated_string <> "+" <> to_string(current_num+1))) | |
explore(concatenated_string <> "-" <> to_string(current_num+1), current_num+1, Code.eval_string(concatenated_string <> "-" <> to_string(current_num+1))) | |
end | |
end | |
Upto100.go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment