Compare commits

...

3 Commits

Author SHA1 Message Date
Nathan Mattes ba73d0040f Add possible solution for StringsAndBinaries-4
I could've used parse.exs from the book, too, I guess?
2021-09-22 12:12:51 +02:00
Nathan Mattes 193d58827f Compare two anagrams
functions that return a boolean seem to end with an ?
2021-09-22 10:55:26 +02:00
Nathan Mattes ff8cd2798a Add two possible solutions to the ascii-printable exercise 2021-09-22 10:49:19 +02:00
1 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,33 @@
defmodule StringsAndBinaries do
def ascii_printable?(char_list) do
List.ascii_printable?(char_list)
end
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))
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))
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