IdeaMonk

thoughts, ideas, code and other things...

Sunday, November 29, 2009

Recursion limit in Python

Doing ACODE today, I realized that my solution was getting a runtime error. While I had tested the code on my system with simple hand typed cases. So even if I had typed out a 500 digit random number to test the code, still it wouldn't run on SPOJ. I then tested it with a real 5000 digit number, and there you go, a nice RuntimeError -
  File "ACODE.py", line 20, in rek
dp[a] = rek(a+1)
File "ACODE.py", line 20, in rek
dp[a] = rek(a+1)
RuntimeError: maximum recursion depth exceeded
Here is a simple solution to increase your recursion limit while doing SPOJ or Euler problems, observe -
>>>
>>> import sys
>>> sys.getrecursionlimit()
1000
>>> sys.setrecursionlimit(6000)
>>> sys.getrecursionlimit()
6000
>>>

So from now on, don't forget a sys.setrecursionlimit( ... ) before you make python dig the deep jungle of recursive function calls.

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home