I have a list B=[0,0,0,0,0,0,0,0,0] but it can be any length.
I am trying to iterate through all possible values I can place in B by iteration. When some condition C is met, I want to “reset” the element I just iterated and bump the next item up by 1. Sort of like binary:
000 becomes 001 but then when we increase to 002, condition C is met so we drop it to 0 and increment the next column: 002 becomes 010, etc.
Sorry if I explained that poorly.
So B might go from
B=[0,0,0,0,1,2,5]
to
B=[0,0,0,0,1,2,6]
to
B=[0,0,0,0,1,2,7]
and so forth.
But when condition C is met, I want to reset in this way:
B=[0,0,0,0,1,2,96]
...attempt to increment
B=[0,0,0,0,1,2,97]
...attempt to increment
Condition C met
B=[0,0,0,0,1,3,0]
And be able to do this until I eventually hit condition C on the far left element (equivalent to hitting 1111111 and being unable to increase it any more).
For the sake of easier coding let’s say condition C = the sum of all the numbers exceeds 100.
My attempt (as requested by agf):
B=[0,0,0,0,0,0,0,0]
lenB=len(B)
while sum(B)<=100: #I think I have to somehow account for having tried incrementing the far left instead
B[lenB-1]+=1 #increment last value in B
while sum(B)>100: #if the sum is greater than 100
B[lenB-1]=0 #reset the far right element
B[lenB-2]+=1 #increment the next element
#but this is wrong because it needs to perform this check again and again
#for every column, while also checking if B[len-1] or B[len-2] even exists
EDIT: My Condition C in reality is MUCH more complex than simply checking if Sum(B)>100. I’m just using this as a dummy condition because I can simply replace “if sum(B)>100” with my more complex conditional function.
Does this do what you want?
Even for this example, we run through over 5000 iterations before [1,1,1]
EDIT
Added a simple
breakstatement as it is only necessary to check the list as long as it changed during the last iteration.