-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
fixed error in factorial.py #1888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,37 +1,37 @@ | ||||||||||||
# 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] | ||||||||||||
""" | ||||||||||||
result = [-1] * (num + 1) | ||||||||||||
|
||||||||||||
if num < 0: | ||||||||||||
return "Number should not be negative." | ||||||||||||
|
||||||||||||
return factorial_aux(num, result) | ||||||||||||
|
||||||||||||
|
||||||||||||
def factorial_aux(num, result): | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type hints? Doctests? |
||||||||||||
|
||||||||||||
if num == 0 or num == 1: | ||||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
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) | ||||||||||||
result[num] = num * factorial_aux(num - 1, result) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, this is more of a "memoization" approach. While being perfectly correct, the more appropriate algorithm for this section may look as follows
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. xrange() was removed from Python on 1/1/2020. The canonical way to do memoization in Python is with the lru_cache function decorator. |
||||||||||||
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 | ||||||||||||
|
||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.