cheran-senthil/PyRival

Error using code

Closed this issue · 1 comments

Python Version: 3

Describe the bug
problem link
My code:

from types import GeneratorType
 
def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
 
    return wrappedfunc
 
n, mod = int(input()), 10**9 + 7
 
dp = [None for _ in range(n+1)]
 
@bootstrap
def recur(n):
	if n <= 0:
		return 0
	elif dp[n] != None:
		return dp[n]
	dp[n] = 0
	for i in range(1, 7):
		dp[n] += recur(n-i) + (n-i == 0)
	dp[n] %= mod
	return dp[n]
 
print(recur(n))
# print(dp)

Verdict : Runtime Error

Traceback (most recent call last):
  File "input/code.py", line 38, in <module>
    print(recur(n))
  File "input/code.py", line 8, in wrappedfunc
    to = f(*args, **kwargs)
  File "input/code.py", line 34, in recur
    dp[n] += recur(n-i) + (n-i == 0)
  File "input/code.py", line 14, in wrappedfunc
    stack.pop()
IndexError: pop from empty list

If I don't use @bootstrap, it works on small testcases (which exceed recursion depth)