Why this code is failing Test Cases 3 to 6? This is Hofstadter's Q-Sequence problem from 'Hard' section. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why this code is failing Test Cases 3 to 6? This is Hofstadter's Q-Sequence problem from 'Hard' section.

https://code.sololearn.com/cDGbFWP7wEOz/?ref=app

9th Feb 2020, 6:25 AM
Peter Parker
Peter Parker - avatar
3 Answers
+ 5
https://www.sololearn.com/Discuss/2119714/?ref=app https://www.sololearn.com/Discuss/2146525/?ref=app edit : Hari Shankar there is one more way to solve this by setting the recursion limit 👍
9th Feb 2020, 6:47 AM
Daljeet Singh
Daljeet Singh - avatar
+ 2
You can evade the recursion problem by coding an iterative version of the algo. It will be quicker and doesn't shut down at depth 993 or what it was. The basic idea: Instead of recreating all the values every time recursively, store them in an array and just access them.
9th Feb 2020, 11:32 AM
HonFu
HonFu - avatar
+ 1
I tried to use numpy array to store values. Because of this I passed test case 4 & 5. But still I am not able to pass test case 6. What can be wrong? Here is the code https://code.sololearn.com/c3TL6Ns605Yw/?ref=app
10th Feb 2020, 5:41 PM
Peter Parker
Peter Parker - avatar