From aa41865856d086ceda975f85aa6ecec75253b386 Mon Sep 17 00:00:00 2001 From: jpg-130 Date: Sat, 29 Jun 2019 15:51:54 +0530 Subject: [PATCH 1/3] adding factorial --- dynamic_programming/factorial.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 dynamic_programming/factorial.py diff --git a/dynamic_programming/factorial.py b/dynamic_programming/factorial.py new file mode 100644 index 000000000000..61c33e24f480 --- /dev/null +++ b/dynamic_programming/factorial.py @@ -0,0 +1,19 @@ +#Factorial of a number using memoization +def factorial(num): + if num<0: + return -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) + return result[num] + +#factorial of num +result=[-1]*10 +result[0]=result[1]=1 +print(factorial(5)) +#uncomment the following to see how recalculations are avoided +# print(factorial(3)) +# print(factorial(7)) \ No newline at end of file From 1011db440b6a14c62b519691699489e2fb29bfc6 Mon Sep 17 00:00:00 2001 From: Jigyasa G <33327397+jpg-130@users.noreply.github.com> Date: Sun, 14 Jul 2019 15:02:05 +0530 Subject: [PATCH 2/3] adding doctest --- dynamic_programming/factorial.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/factorial.py b/dynamic_programming/factorial.py index 61c33e24f480..c2119adcf1f1 100644 --- a/dynamic_programming/factorial.py +++ b/dynamic_programming/factorial.py @@ -1,7 +1,16 @@ #Factorial of a number using memoization 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 -1 + return "Number should not be negative." if result[num]!=-1: return result[num] else: @@ -11,9 +20,16 @@ def factorial(num): return result[num] #factorial of num -result=[-1]*10 -result[0]=result[1]=1 -print(factorial(5)) #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)) \ No newline at end of file +# print(factorial(7)) + + +if __name__ == "__main__": + import doctest + result=[-1]*10 + result[0]=result[1]=1 + doctest.testmod() From ba98234d31a01a360b66d6de1b28fa0b423c1422 Mon Sep 17 00:00:00 2001 From: Jigyasa G <33327397+jpg-130@users.noreply.github.com> Date: Wed, 17 Jul 2019 11:36:02 +0530 Subject: [PATCH 3/3] Update factorial.py --- dynamic_programming/factorial.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/factorial.py b/dynamic_programming/factorial.py index c2119adcf1f1..7c6541ee2a74 100644 --- a/dynamic_programming/factorial.py +++ b/dynamic_programming/factorial.py @@ -1,4 +1,6 @@ #Factorial of a number using memoization +result=[-1]*10 +result[0]=result[1]=1 def factorial(num): """ >>> factorial(7) @@ -27,9 +29,6 @@ def factorial(num): # print(factorial(3)) # print(factorial(7)) - if __name__ == "__main__": import doctest - result=[-1]*10 - result[0]=result[1]=1 doctest.testmod()