Problem
I am aware that somewhere in my function, I am not returning something I should.
I am returning the recursive call, but it appears that I am not returning "all the way out"
Context
I am doing a depth first search of every single combination in a list. Once I reach a combination that reaches a condition, I want to return.
I am maintaining "state" of my combination and am backtracking where I should be (I think).
What am I doing wrong?
class Combo:
def __init__(self, list):
self.staples = list
Combo has a property called "staples", consisting of a list of staple classes. I want to iterate over the list of staples in a decision tree to find an optimal number.
In this case, the optimal number is summed across the quantities of each staple instance in the list and stored/recalculated as a property on the Combo instance.
def IterateStaples(combo, target):
#Exit condition for combo dictionary
if all(combo.diff[macro] < 2 for macro in combo.diff):
return combo;
#iterate through all items in list
for staple in combo.staples:
#Increment and calc conditions
staple.increment()
combo.calcTotals()
combo.findDiff(target)
#If exceeds target value, backtrack
if combo.findConflict(target):
staple.decrement()
combo.calcTotals()
combo.findDiff(target)
#Redundant exit condition to try and return
elif all(combo.diff[macro] < 2 for macro in combo.diff):
return combo
#Recursive call
else:
return IterateStaples(combo, target)
staple.decrement()
combo.calcTotals()
combo.findDiff(target)