Tuesday 15 March 2011

python - How can I refactor this line of code: -



python - How can I refactor this line of code: -

i'm writing programme check if sudoku solved. here part can't think of way refactor:

for in range(0, 9): j in range(0, 9): if < 3: if j < 3: one.append(sudoku[i][j]) if j >= 3 , j < 6: two.append(sudoku[i][j]) if j >= 6: three.append(sudoku[i][j]) if >= 3 , < 6: if j < 3: four.append(sudoku[i][j]) if j >= 3 , j < 6: five.append(sudoku[i][j]) if j >= 6: six.append(sudoku[i][j]) if >= 6: if j < 3: seven.append(sudoku[i][j]) if >= 6 , j >= 3 , j < 6: eight.append(sudoku[i][j]) if j >= 6: nine.append(sudoku[i][j])

thanks!

try using 2d array hold objects , map them follows:

mapping = [[one, two, three], [four, five, six], [seven, eight, nine]] in range(0, 9): j in range(0, 9): square = mapping[i/3][j/3] square.append(sudoku[i][j])

if using python 3, replace square = mapping[i/3][j/3] square = mapping[int(i/3)][int(j/3)] or square = mapping[i//3][j//3] padraic pointed out

the reason python2 round downwards , homecoming int division, python3 homecoming float. casting int round down.

python if-statement refactoring

No comments:

Post a Comment