mix format strings-exercises

This commit is contained in:
Nathan Mattes 2021-09-28 12:06:21 +02:00
parent 2eef0ae2d6
commit 0407e44ea7
1 changed files with 17 additions and 16 deletions

View File

@ -5,27 +5,28 @@ defmodule StringsAndBinaries do
def custom_ascii_printable?(char_list) do
# every character in char list must between 32 and 126
Enum.all?(char_list, &( 32 <= &1 and &1 <= 126))
Enum.all?(char_list, &(32 <= &1 and &1 <= 126))
end
def anagram?(word1, word2) 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))
# 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
# 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