Recursion Issue is Killing Me! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

7th Apr 2020, 1:46 AM
DAYLINER BAND
DAYLINER BAND - avatar
4 Answers
+ 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
7th Apr 2020, 6:47 AM
Louis
Louis - avatar
+ 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
7th Apr 2020, 5:51 AM
Tibor Santa
Tibor Santa - avatar
+ 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.
7th Apr 2020, 11:02 AM
DAYLINER BAND
DAYLINER BAND - avatar
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.
11th Apr 2020, 10:54 AM
DAYLINER BAND
DAYLINER BAND - avatar