ihsancemil
ihsancemil

Reputation: 432

Sudoku with python

def print_map(sudoku_map):
    for line in sudoku_map:
        print(line)
        print("\n") 

#it will determine whether there is a same a in row or not
def search_row(sudoku_map_search, a, row):
    for i in range(9):
        if sudoku_map_search[row][i] == a:
            return 0;
        else:
            return 1;

#it will determine whether there is a same a in column or not
def search_column(sudoku_map_search, a, column):
    for b in range(9):
        if sudoku_map_search[b][column] == a:
            return 0;
        else:
            return 1;

sudoku_map = [
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],

[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0]
];

for row in range(9):
    for column in range(9):
        #if block is empty loop will place a number;
        if sudoku_map[row][column]==0:
            #a will be a number which will try all the numbers between 0-10 for blank places
            for a in range(1,10):
                if search_row(sudoku_map, a ,row)==1 and search_column(sudoku_map, a, column)==1:
                    sudoku_map[row][column]= a

print_map(sudoku_map)

I'm aiming to print a map which is looks like:

  1. 9 8 7 6 5 4 3 2 1
  2. 8 7 6 5 4 3 2 1 9
  3. 7 6 5 4 3 2 1 9 8
  4. 6 5 4 3 2 1 9 8 7
  5. 5 4 3 2 1 9 8 7 6
  6. 4 3 2 1 9 8 7 6 5
  7. 3 2 1 9 8 7 6 5 4
  8. 2 1 9 8 7 6 5 4 3
  9. 1 9 8 7 6 5 4 3 2

but I couldn't figure it out why it just printing:

  1. 9 8 8 8 8 8 8 8 8
  2. 8 9 9 9 9 9 9 9 9
  3. 8 9 9 9 9 9 9 9 9
  4. 8 9 9 9 9 9 9 9 9
  5. 8 9 9 9 9 9 9 9 9
  6. 8 9 9 9 9 9 9 9 9
  7. 8 9 9 9 9 9 9 9 9
  8. 8 9 9 9 9 9 9 9 9
  9. 8 9 9 9 9 9 9 9 9

Do you have any idea why I'm unable to reach my aim?

Upvotes: 2

Views: 193

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

You can use a generator expression using any which will return 1 or 0 i.e True or False. Your code looks like your are attempting to write c not python, in python you can iterate over the elements of a list without indexing and you don't need semicolons.

Your function can be simplified to:

def search_row(sudoku_map_search, a, row):
    return any(ele == a for ele in sudoku_map_search[row])

any lazily evaluate short circuiting on any ele == a being True, if no match is found it will simply return False.

To check if there was a match you simply need:

 if search_row(sudoku_map, a ,row):
     sudoku_map[row][column] = a

You don't need to explicitly check the return value with ==.

You can also simplify your print function using str.join:

def print_map(sudoku_map):
    print("\n".join(sudoku_map))

Or using print as a function:

# needed for python2
from __future__ import print_function

def print_map(sudoku_map):
    print(*sudoku_map, sep="\n")

Upvotes: 1

kilojoules
kilojoules

Reputation: 10083

Use the else with the for loop in your search functions. This way, 1 is only returned if no iteration returned a break. You could even simply return 1 after the for loop.

#it will determine whether there is a same a in row or not
def search_row(sudoku_map_search, a, row):
    for i in range(9):
        if sudoku_map_search[row][i] == a:
            return 0;
    else:
        return 1;

Or just return 1 after the for loop. 1 will only be returned if no iteration was successful.

#it will determine whether there is a same a in row or not
def search_row(sudoku_map_search, a, row):
    for i in range(9):
        if sudoku_map_search[row][i] == a:
            return 0;
    return 1;

Upvotes: 2

Related Questions