why doesnt it work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why doesnt it work?

board = [ [7,8,0,4,0,0,1,2,0], [6,0,0,0,7,5,0,0,9], [0,0,0,6,0,1,0,7,8], [0,0,7,0,4,0,2,6,0], [0,0,1,0,5,0,9,3,0], [9,0,4,0,6,0,0,0,5], [0,7,0,3,0,0,0,1,2], [1,2,0,0,0,7,4,0,0], [0,4,9,2,0,6,0,0,7] ] def solve(bo): for column in bo: for n, i in enumerate(column): if i == 0: #Find the box that the epty space is in pos_x = (column.index(i) // 3) * 3 pos_y = (bo.index(column) // 3) * 3 #insert numbers inside empty space for k in range(1, 10): if validation(k, column, bo, pos_x, pos_y) == True: column[n] = k return print_board(bo) def validation(num, col, game_board, box_x, box_y): if num in col: return False else: box_nums = [] for x in range(0, 3): box_nums.append(game_board[box_y][box_x * 3:(box_x * 3) + 3]) box_y += 1 if num in box_nums[0] or box_nums[1] or box_nums[2]: box_nums.clear() return False return True def print_board(the_board): for k in the_board: print(k) print(solve(board)) ---------------------------------- line 22 never works ps:the code is still not finished

15th Sep 2020, 5:21 PM
Kirill
Kirill - avatar
15 Answers
+ 6
Not sure which line is number 22, but I'm pretty sure the line if num in box_nums[0] or box_nums[1] or box_nums[2]: doesn't do what you intended. If you want to check if num is in box_nums[0] or num is in box_nums[1] or num is in box_nums[2], you have to express it just like that. if num in box_nums[0] or num in box_nums[1] or num in box_nums[2]: ...
15th Sep 2020, 5:54 PM
Russ
Russ - avatar
+ 4
Kirill Vidov This is how I would rewrite your solve() function: def solve(bo): for n, row in enumerate(bo): for m, cell in enumerate(row): if cell == 0: pos_x = n - n%3 pos_y = m - m%3 for k in range(1, 10): if validation(...): row[m] = k I realise you said the code is still not finished, but I don't think you have done enough in the validation function. You have checked if the number appears in the same row (you have used the word column, but it is actually the row), and if it appears in the same 3x3 block, but not if it appears in the same column. Feel free to take a look at a checker I wrote a while back. Note, this is obviously just a checker, not a solver. https://code.sololearn.com/c455G0Sdzi3i/?ref=app
15th Sep 2020, 7:15 PM
Russ
Russ - avatar
+ 1
Great! 👍
15th Sep 2020, 6:01 PM
Russ
Russ - avatar
+ 1
Jhonie San Juan dont worry about it, Russ solved my problem, but thanks
15th Sep 2020, 7:28 PM
Kirill
Kirill - avatar
+ 1
Russ greatly appreciated, thank you once again
15th Sep 2020, 7:28 PM
Kirill
Kirill - avatar
+ 1
Yes, it currently only checks rows, not columns.
15th Sep 2020, 7:57 PM
Russ
Russ - avatar
+ 1
Russ my bad, i got confused with rows and columns
15th Sep 2020, 7:59 PM
Kirill
Kirill - avatar
0
Russ omg that worked
15th Sep 2020, 5:59 PM
Kirill
Kirill - avatar
0
Russ thank you so much
15th Sep 2020, 5:59 PM
Kirill
Kirill - avatar
0
Russ is there a way to make it shorter?
15th Sep 2020, 6:01 PM
Kirill
Kirill - avatar
0
import numpy board = [ [7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 0, 0, 0, 7, 5, 0, 0, 9], [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 0, 7, 0, 4, 0, 2, 6, 0], [0, 0, 1, 0, 5, 0, 9, 3, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5], [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 7, 4, 0, 0], [0, 4, 9, 2, 0, 6, 0, 0, 7] ] def check(y, x, n): global board for i in range(0, 9): if board[y][i] == n: return False for i in range(0, 9): if board[y][i] == n: return False xO = (x // 3) * 3 yO = (y // 3) * 3 for i in range(0, 3): for j in range(0, 3): if board[yO + i][xO + i] == n: return False return True def solve(): global board for y in range(9): for x in range(9): if board[y][x] == 0: for n in range(1, 10): if check(y, x, n): board[y][x] = n solve() board[y][x] = 0 return print(numpy.matrix(board)) exit(0) solve()
15th Sep 2020, 6:28 PM
Jhonie San Juan
Jhonie San Juan - avatar
0
Jhonie San Juan nice one, but I want to make an original solver
15th Sep 2020, 6:59 PM
Kirill
Kirill - avatar
0
Ah, okie nice. You want this to be dynamic. I actually got that from youtube somewhere in the past so i just searched it again. I will read that again so i may understand what you want. Thanks
15th Sep 2020, 7:26 PM
Jhonie San Juan
Jhonie San Juan - avatar
0
Russ for now I only checked if it appears in the same line horizontally, I need to check vertically
15th Sep 2020, 7:37 PM
Kirill
Kirill - avatar
- 1
you need to import "numpy" for that to work
15th Sep 2020, 5:41 PM
Jhonie San Juan
Jhonie San Juan - avatar