Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmarreck/9bff88bfbd27e7b552f1 to your computer and use it in GitHub Desktop.
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
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