4 ms·
It's cool in a novelty way that it’s so short, but I would infinitely prefer something like this for actual work and understanding: def solve(grid): de
by eigenvalue 2y ago
It's cool in a novelty way that it’s so short, but I would infinitely prefer something like this for actual work and understanding:
def solve(grid):
def find_empty(grid):
for r in range(9):
for c in range(9):
if grid[r][c] == 0:
return r, c
return None
def is_valid(grid, num, pos):
r, c = pos
if num in grid[r]:
return False
if num in [grid[i][c] for i in range(9)]:
return False
box_r, box_c = r // 3 * 3, c // 3 * 3
for i in range(box_r, box_r + 3):
for j in range(box_c, box_c + 3):
if grid[i][j] == num:
return False
return True
def backtrack(grid):
empty = find_empty(grid)
if not empty:
return True
r, c = empty
for num in range(1, 10):
if is_valid(grid, num, (r, c)):
grid[r][c] = num
if backtrack(grid):
return True
grid[r][c] = 0
return False
backtrack(grid)
return grid
- upghost 2y agoWhy is this getting down-voted without comment? Comparative analysis is taboo, now? I don't think Arthur Whitney would feel the least bit threatened by some Python code.
- eigenvalue 2y agoThe K-mafia is in control. Just kidding, I don’t really care either way…
- BoiledCabbage 2y agoSpeculation, but maybe because there is nothing of interest or to note in the comment. It's not clear why the poster prefers that other implementation, or that they understand APL or array programming. So as a result the comment reads as "it's in a language I don't know. I'd prefer it in a language I do know." Which is a fairly useless comment. If that's not what they intended, it would be helpful for them to add some context to their comment.