mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +08:00
psf/black code formatting (#1277)
This commit is contained in:

committed by
Christian Clauss

parent
07f04a2e55
commit
9eac17a408
@ -1,6 +1,8 @@
|
||||
#Factorial of a number using memoization
|
||||
result=[-1]*10
|
||||
result[0]=result[1]=1
|
||||
# Factorial of a number using memoization
|
||||
result = [-1] * 10
|
||||
result[0] = result[1] = 1
|
||||
|
||||
|
||||
def factorial(num):
|
||||
"""
|
||||
>>> factorial(7)
|
||||
@ -10,19 +12,20 @@ def factorial(num):
|
||||
>>> [factorial(i) for i in range(5)]
|
||||
[1, 1, 2, 6, 24]
|
||||
"""
|
||||
|
||||
if num<0:
|
||||
|
||||
if num < 0:
|
||||
return "Number should not be negative."
|
||||
if result[num]!=-1:
|
||||
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)
|
||||
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
|
||||
|
||||
# factorial of num
|
||||
# uncomment the following to see how recalculations are avoided
|
||||
##result=[-1]*10
|
||||
##result[0]=result[1]=1
|
||||
##print(factorial(5))
|
||||
@ -31,4 +34,5 @@ def factorial(num):
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
Reference in New Issue
Block a user