mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
1f8a21d727
* Tighten up psf/black and flake8 * Fix some tests * Fix some E741 * Fix some E741 * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
39 lines
836 B
Python
39 lines
836 B
Python
# Factorial of a number using memoization
|
|
result = [-1] * 10
|
|
result[0] = result[1] = 1
|
|
|
|
|
|
def factorial(num):
|
|
"""
|
|
>>> factorial(7)
|
|
5040
|
|
>>> factorial(-1)
|
|
'Number should not be negative.'
|
|
>>> [factorial(i) for i in range(5)]
|
|
[1, 1, 2, 6, 24]
|
|
"""
|
|
|
|
if num < 0:
|
|
return "Number should not be negative."
|
|
if result[num] != -1:
|
|
return result[num]
|
|
else:
|
|
result[num] = num * factorial(num - 1)
|
|
# uncomment the following to see how recalculations are avoided
|
|
# print(result)
|
|
return result[num]
|
|
|
|
|
|
# factorial of num
|
|
# uncomment the following to see how recalculations are avoided
|
|
# result=[-1]*10
|
|
# result[0]=result[1]=1
|
|
# print(factorial(5))
|
|
# print(factorial(3))
|
|
# print(factorial(7))
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|