I've been given the following problem and my code has become a horrible mess and I'm quite stuck.
You are given an allowance to spend at some shops (something like $15, just some positive integer) You may only spend this money under two conditions:
1) You buy exactly one item from each shop.
2) You spend all of your money (nothing left over, and no debt)
Find all possible ways you could have purchased items to satisfy the above.
In actuality, you're given a budget int and some array like
9 and [[1,2,3],[0,5],[2,3,8]]
where each inner array lists the prices of items in the a shop. There can be arbitrarily many shops with arbitrarily many items. Costs cannot be negative, but they can be free!
The expected solution here would be:
[[1,5,3],[2,5,2],[1,0,8]]
As each array contains one item from each shop, each totals 9, and all possibilities are present. Just to make it more difficult, speed is paramount.
The following is my code that has descended into madness and a near complete lack of functionality:
def Stepper(bud,LOL):
n=len(LOL)
lasts=[]
indices=[0 for j in range(n)]
focus=0
funds=[0 for j in range(n+1)]
funds[0]=bud
sols=[]
moveable=[]
for j in range(n):
length=len(LOL[j])
if(length==0):
return []
lasts.append(length-1)
if(moveable==[]):
if(length==1):
funds[j+1]=funds[j]-LOL[j][0]
else:
moveable.append(j)
focus=j
while(moveable!=[]):
while(LOL[focus][indices[focus]] > funds[focus]):
indices[focus]+=1
if(indices[focus]==lasts[focus]):
if(moveable[-1]==focus):
moveable.remove(focus)
if(moveable==[]):
if(focus<n-1):
moveable.append(focus+1)
funds[focus+1]=funds[focus]-LOL[focus][indices[focus]]
#print(n,lasts,indices,focus,moveable,funds,sols)
if((funds[focus+1]!=0) and (focus<n-1)):
focus+=1
indices[focus]=0
else:
if(funds[focus+1]==0):
for j in range(focus+1,n):
indices[j]=lasts[j]
sols.append(list(indices))
if(moveable[-1]==n-1):
moveable.remove(n-1)
if(moveable!=[]):
focus=moveable[-1]
indices[focus]+=1
if(indices[focus]==lasts[focus]):
if(moveable[-1]==focus):
moveable.remove(focus)
if(moveable==[]):
if(focus<n-1):
moveable.append(focus+1)
funds[focus+1]=funds[focus]-LOL[focus][indices[focus]]
focus+=1
indices[focus]=0
return(sols)
where bud is the budget and LOL is the list of lists (the shops and prices)