3 ms·
The "Python expert" version doesn't run, but it's not hard to fix: import operator as op import functools as f fact = lambda x: f.reduce(op.mul, range(1,
by tomku 15y ago
The "Python expert" version doesn't run, but it's not hard to fix:
import operator as op
import functools as f
fact = lambda x: f.reduce(op.mul, range(1, x + 1))
print(fact(6))
If you're using Python 2.x, you can make it a bit shorter due to reduce being in the default namespace:
import operator as op
fact = lambda x: reduce(op.mul, xrange(1, x + 1))
print fact(6)