by z324werwer · 14y ago · 0 comments
in ruby
def fact(n)
n == 0 ? 1 : n.downto(1).inject(:*)
end
def pascal(n)
0.upto(n) {|r| print "#{fact(n)/(fact(r)*fact(n - r))} "}
endlet step row = zipWith (+) (0:row) (row++[0])
let triangle = iterate step [1]
take 10 triangle
2. I think an iterative function for fact would be more efficient.
3. I notice you do not memoize the result of fact even though you are calculating it twice for each call.
I suspect that you are trying to show how compact a solution can be, but in an interview situation, I would probably be more impressed by someone who is aware of the issues above.