0

im learning python, I wanted the sudoku numbers to be read from a csv file and to be reconstructed. that part works. but it does not solve the sudoku. it just prints the same input again. Can somebody please help me? Any idea why?

Question number 2: Any Tipps how i could modify this code to give the number of possible sudoku solutions? In case, there are multiple solutions possible.

Thank you so much in Advanced! :)

import csv

with open('sudoko.csv') as f:
    board = list(csv.reader(f,delimiter=';'))
     
def solve(bo):
    find = find_empty(bo)
    if not find:
        return True
    else:
        row, col = find

    for num in range(1,10):
        if valid(bo, num, (row, col)):
            bo[row][col] = num

            if solve(bo):
                return True

            bo[row][col] = 0
    return False

def valid(bo, num, pos):
    # Check row
    for field in range(len(bo[0])):
        if bo[pos[0]][field] == num and pos[1] != field:
            return False

    # Check column
    for line in range(len(bo)):
        if bo[line][pos[1]] == num and pos[0] != line:
            return False

    # Check box
    box_x = pos[1] // 3
    box_y = pos[0] // 3

    for i in range(box_y*3, box_y*3 + 3):
        for j in range(box_x * 3, box_x*3 + 3):
            if bo[i][j] == num and (i,j) != pos:
                return False

    return True


def print_board(bo):
    for i in range(len(bo)):
        if i % 3 == 0 and i != 0:
            print("- - - - - - - - - - - - - ")

        for j in range(len(bo[0])):
            if j % 3 == 0 and j != 0:
                print(" | ", end="")

            if j == 8:
                print(bo[i][j])
            else:
                print(str(bo[i][j]) + " ", end="")


def find_empty(bo):
    for i in range(len(bo)):
        for j in range(len(bo[0])):
            if bo[i][j] == 0:
                return (i, j)  # row, col

    return None

if __name__ == "__main__":
    print_board(board)
    solve(board)
    print("___________________")
    print("")
    print_board(board)
chaos
  • 5
  • 5
  • csv.reader will give you a list of lists of string values. The code doesn't convert these strings to ints. So in the find_empty() function, bo[i][j] will never be the integer 0 and the return value will always be None. So solve() returns True the first time it's called, and that's why you're just seeing the original board be printed. – slothrop Jul 02 '22 at 11:38
  • Thanks so much for your Comment! it makes so much sense now. I tried so many possibilities to convert it to integer but none of them work. i always get these kind of errors --> invalid literal for int() with base 10:. What am i doing wrong? – chaos Jul 02 '22 at 13:12
  • You could try something like: board_as_ints = [[int(col) for col in row] for row in board] – slothrop Jul 02 '22 at 13:17
  • it also does not work ... --> ValueError: invalid literal for int() with base 10: '\ufeff7'. somehow strange.. don't know where the \ufeff7 comes from ... as my csv.file contains numbers only. but thanks for your help. – chaos Jul 02 '22 at 13:35
  • You probably need to specify the encoding of the file as 'utf-8-sig' when opening it: https://stackoverflow.com/questions/17912307/u-ufeff-in-python-string – slothrop Jul 02 '22 at 14:08

0 Answers0