Add possible solution for StringsAndBinaries-4

I could've used parse.exs from the book, too, I guess?
This commit is contained in:
Nathan Mattes 2021-09-22 12:12:51 +02:00
parent 193d58827f
commit ba73d0040f
1 changed files with 18 additions and 0 deletions

View File

@ -12,4 +12,22 @@ defmodule StringsAndBinaries do
# An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Enum.sort(String.to_charlist(word1)) == Enum.sort(String.to_charlist(word2))
end
def calculate(expression) do
# this might be a bit dirty, but it solves the issue.
[a, operator, b] = ~w[ #{expression} ]
a = String.to_integer(a)
b = String.to_integer(b)
case operator do
"+" -> add(a, b)
"-" -> substract(a, b)
"*" -> multiply(a, b)
"/" -> divide(a, b)
end
end
defp add(a, b), do: a + b
defp substract(a, b), do: a - b
defp multiply(a, b), do: a * b
defp divide(a, b), do: a / b
end