Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upfixed error in factorial.py #1888
Conversation
us3rnotfound left a comment
Recommend making factorial_aux a private function since no outside entity need use it. Recommend putting the debug statements back in showing where to print result for debugging, although I noticed debugging it in that fashion causes docttest to print out a lot of example failed blocks. Otherwise I think this change is good and it makes sense. |
TravisBuddy
commented
Jun 23, 2020
Hey @SandersLin, TravisCI finished with status TravisBuddy Request Identifier: 09604660-b535-11ea-aae7-0b05fcc524af |
pawarashish564
commented
Jul 8, 2020
we can use |
return factorial_aux(num, result) | ||
|
||
|
||
def factorial_aux(num, result): |
This comment has been minimized.
This comment has been minimized.
pawarashish564 left a comment
Its working .! |
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) |
This comment has been minimized.
This comment has been minimized.
fhlasek
Aug 5, 2020
Contributor
Technically, this is more of a "memoization" approach. While being perfectly correct, the more appropriate algorithm for this section may look as follows
result[num] = num * factorial_aux(num - 1, result) | |
result[0] = 1 | |
for i in xrange(num): | |
result.append((i + 1) * result[i]) | |
return result[num] |
This comment has been minimized.
This comment has been minimized.
cclauss
Aug 5, 2020
Member
xrange() was removed from Python on 1/1/2020.
The canonical way to do memoization in Python is with the lru_cache function decorator.
TravisBuddy
commented
Aug 5, 2020
Hey @SandersLin, TravisCI finished with status TravisBuddy Request Identifier: aabb4010-d708-11ea-85fc-4b61cecd2c91 |
SandersLin commentedApr 17, 2020
•
edited
Describe your change:
factorial(11)
returnsindex out of range
. To implement memoization an auxiliary function is required.Checklist:
Fixes: #{$ISSUE_NO}
.