I'm trying for fun to solve problems for Google Kickstart 2020, Round A, using Python 3.7. I passed all the problems except the "Bundling" one. My algorithm passes the samples, but fails the tests with a runtime error. I tested it also with big test cases that I build with a simple script, and it works, without rising any type of exception.
I also thought that, since I'm using Python 3.8 on my machine, while Google tests the scripts with 3.7, this could be the point... but I cant' find anything of wrong in my code. Here is my code:
ans = 0
class Node:
def __init__(self):
self.children = {}
self.count = 0
def insert(node, key):
for char in key:
idx = char
if idx not in node.children:
node.children[idx] = Node()
node = node.children[idx]
node.count +=1
def resetAns(self):
global ans
ans = 0
def dfs(self, dep=0, k=0):
global ans
for c in self.children:
self.children[c].dfs(dep+1, k)
self.count+=self.children[c].count
while self.count >= k:
self.count -= k
ans+=dep
return ans
def bundling():
N, K = map(int, input().split())
_node = Node()
_node.resetAns()
for _ in range(0, N):
_v = input()
_node.insert(_v)
return _node.dfs(0, K)
for _ in range(int(input())):
print("Case #{}: {}".format(_, bundling()))
Any help is welcome :D
The problem has occurred because of the small default recursion depth limit python provides. You can set the limit explicitly using the below code.
import sys
sys.setrecursionlimit(10**6)