Thanks! Your solution also works well here. To improve readability, parameters to check_exam/3 could be named explicitly e.g. right instead of a and wrong instead of b. Also unused parameters should be marked with _.
check_exam/3
right
a
wrong
b
_
Something like
def check_exam([right | correct], [right | answer], score), do: check_exam(correct, answer, score + 4) def check_exam([_right | correct], ["" | answer], score), do: check_exam(correct, answer, score) def check_exam([_right | correct], [_wrong| answer], score), do: check_exam(correct, answer, score - 1)
Thanks to both of you. The combination of your solution looks great!
defmodule CheckExam do def check_exam(correct, answer, score \\ 0) def check_exam([right | correct], [right | answer], score), do: check_exam(correct, answer, score + 4) def check_exam([_right | correct], ["" | answer], score), do: check_exam(correct, answer, score) def check_exam([_right | correct], [_wrong| answer], score), do: check_exam(correct, answer, score - 1) def check_exam([], [], score) when score < 0, do: 0 def check_exam([], [], score), do: score end CheckExam.check_exam(["a", "a", "b", "b"], ["a", "c", "b", "d"]) CheckExam.check_exam(["a", "a", "c", "b"], ["a", "a", "b", ""]) CheckExam.check_exam(["a", "a", "b", "c"], ["a", "a", "b", "c"]) CheckExam.check_exam(["b", "c", "b", "a"], ["", "a", "a", "c"])
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks! Your solution also works well here. To improve readability, parameters to
check_exam/3
could be named explicitly e.g.right
instead ofa
andwrong
instead ofb
. Also unused parameters should be marked with_
.Something like
Thanks to both of you. The combination of your solution looks great!