I am attempting to create a nested for loop where the inner loop will have a different range the first time the loop runs and then use a different range for each subsequent loop.
The program is a sudoku solver. And it works by taking a position on the 9x9 board (board[k][l]), checking a condition, and then moving to the board position directly to the left (board[k][l-1]).
If l is 0 than we need to move to the previous row (k-1) and the farthest position to the right where l equals 8.
The problem I am having is on the first iteration of the inner loop the loop will not always begin with l equal to 8.
For example a user my select the square board[3][3].
The function should then check board[3][2]
then board[3][1]
then board[3][0]
then board[2][8]
etc.
The code below only works if l=8
for i in range(k, -1, -1):
for j in range(l, -1, -1):
For clarity, I can achieve the desired result using multiple for loops, but I am trying to make this code more concise:
k = user selection
l = user selection
for j in range(l, 0, -1):
test(k,j)
for i in range(k-1, -1, -1):
for j in range(9, 0 , -1):
test(i,j)
I don't like this for two reasons, first we encounter a problem if either k or l starts at 0, second it seems unnecessary to use two for loops here.