4 ms·
There’s an easy way to figure out if recursion is fine for your usecase def isRecursionOk(problem) for (subProblem in (breakDownIntoSubProblems(probl
by jameshart 3y ago
There’s an easy way to figure out if recursion is fine for your usecase
def isRecursionOk(problem)
for (subProblem in (breakDownIntoSubProblems(problem))
if ! isRecursionOk(subProblem) return false
return true
- Joel_Mckay 3y ago"kernel: Out of memory: Kill process 9163 (Insulin-pump-task) score 511 or sacrifice child" There are safer hobbies available like snake juggling =)
- hughdbrown 3y agoThis code terminates only if all child-problems eventually are found not to have subproblems. So either it has infinite subproblems or it returns true. So recursion is good for a use case unless the problem decomposes into infinite subproblems, but in that case this test function never returns false. Makes more sense to do it as breadth-first with only one return value: from collections import deque def isRecursionOk(problem): q = deque(problem) while q: problem = q.popleft() q.append(list(breakDownIntoSubProblems(problem))) return true