+ 1
Recursion Issue is Killing Me!
I can't seem to get this program to work for larger numbers > 30. I have tried several ways to free up memory but maybe the function is too difficult. Notvsure what to do so any help would be great. https://code.sololearn.com/ccHyW99Ir1op/?ref=app
5 ответов
+ 3
You are creating a global dict hof_cache , but you are never using it.
firstly, create the hof_cache with the seed values {1:1,2:1}
then, in your function, you must always return the hof_cache[n] .
So invert your logic.
Only when the value is not in hof_cache , all you do is update hof_cache at that value using recursion.
End with returning hof_cache[n]
P. S.
thanks Tibor Santa
I never knew about
from functools import lru_cache
@lru_cache(maxsize=100)
That is definitely the more Pythonic way.
That is what is so cool about this platform. We learn from each other.
https://code.sololearn.com/cml225Rhl4X4/?ref=app
+ 2
I see you were trying to experiment with lru_cache, that is a good direction. You actually have to put the decorator directly above the function (def line). Here is a similar piece of code I wrote a while ago.
https://code.sololearn.com/c578o3uoLhym/?ref=app
+ 1
Thank you all so much for the help, I can now get the program to pass all but the last two test. The inputs can only be positive integers and outputs must be positive integers. I looks like it should pass all the test but I must be missing something.
0
Just in case anyone is interested, I finally got this to work with your suggestions. I had to use what Lewis suggested. The lru_cache method would only get me to 500. Thanks again for all your help.