Python stop recursion once solution is found -
i have recursive function tries form sum given list of integers. function works gives me possible solutions. want break out of recursive function once solution found. how can that? below (pseudo-)code function:
function find_sum(list_of_integers, target_sum, partial_solution): if partial_solutions == target_sum: return true if partial_solution > target_sum: return in range(len(list_of_integers)): n = list_of_integers[i] remaining = list_of_integers[i+1:] find_sum(remaining, target_sum, partial_solution + n)
so want know if target_sum can formed list of integers, don't need solutions.
you need check return value of recursive call; if true
returned, propagate rather continue loop:
for in range(len(list_of_integers)): n = list_of_integers[i] remaining = list_of_integers[i+1:] found = find_sum(remaining, target_sum, partial_solution + n) if found: return true
Comments
Post a Comment