Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm creating a sudoku solver with backtracking. Now, I need to add 1 to each cell. I'm using a "for" loop:

board = [[[0],[0],[0],[0]],
          [[0],[0],[0],[0]],
          [[0],[0],[0],[0]],
          [[0],[0],[0],[0]]]`

a=0
b=0
x=board[a][b]`

for i in x:
    x.append(i+1)
    y = [sum(x)]
    print(y)
    break
board[a][b] = y

What happens is that, if I have a number on the board, like "2", it does the operation of (1+1)+(1+1) +1, and returns 5 instead of 3. I get 9 from 4 [(1+1)+(1+1)+(1+1)+(1+1)+1)], etc.

I'm thinking about solving it by converting "x" from a list to an integer, then adding 1 and then reconverting it in a list.

Any more efficient and time saving ideas?

(I want to keep, if possible, the list structure of "board", but I'm open to any suggestion, of course.)

share|improve this question
    
just to clarify, you want all the cells to contain 1 instead of 0 after the loop? –  aseeon 7 mins ago

1 Answer 1

I would pass the number 1 into x.append(), that should accumulate a one in each cell.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.