Nice work on this one. Here are my notes on your code:
- Your
factorial_iterative
method does the job concisely, but not as concisely as possible. Specifically, yourif
condition only handles zero, but not 1. Since you already haveif n == 0
you might as well catch1
with this line also, so a good refactor would beif n <= 1
. That way, ifn
is1
you don't end up creating an array of[1]
and runningreduce
on it. - Your
reduce
is super sharp. Sweet! - Your
factorial_recursive
method is short and sweet, and it takes care of0
and1
all in one line. - Watch out though: you have a
p
call inside your method. This method should not concern itself with printing the value, only with returning the value. It looks like you are already aware of this because you also have ap
call in your runner code (which is where it should be).
Any questions, let me know.
-Phil