| | |
sudoku solver problem
Thread Solved |
hii everybody,
I have a weird problem with a program for solving sudoku puzzles. I am representing the puzzle as a list of lists of lists containing all the possibillities for a single square and my problem is that when I try to remove a single appearance of a number from a square, the number is removed from all the squares in the grid. I am using the .remove() function and I am not sure why it is behaving like that. Here is the code I am having trouble with...
def eliminate( grid, square, value ):
row = square[ 0 ]; col = square[ 1 ]
poss = grid[ row ][ col ]
if len( poss ) > 1:
poss.remove( value )
grid[ row ][ col ] = poss
# if len( poss ) == 1:
# fixValue( grid, square, poss[ 0 ] )
'square' is a tuple representing the ( row, column ) of the square
'grid' is the whole grid
'value' is the value to be removed from the possibillities for the current square
At the end when I try to print the grid all the squares are changed with the result for 'poss'
If you need other pieces of the code I'll post them here...
I have a weird problem with a program for solving sudoku puzzles. I am representing the puzzle as a list of lists of lists containing all the possibillities for a single square and my problem is that when I try to remove a single appearance of a number from a square, the number is removed from all the squares in the grid. I am using the .remove() function and I am not sure why it is behaving like that. Here is the code I am having trouble with...
def eliminate( grid, square, value ):
row = square[ 0 ]; col = square[ 1 ]
poss = grid[ row ][ col ]
if len( poss ) > 1:
poss.remove( value )
grid[ row ][ col ] = poss
# if len( poss ) == 1:
# fixValue( grid, square, poss[ 0 ] )
'square' is a tuple representing the ( row, column ) of the square
'grid' is the whole grid
'value' is the value to be removed from the possibillities for the current square
At the end when I try to print the grid all the squares are changed with the result for 'poss'
If you need other pieces of the code I'll post them here...
Last edited by masterofpuppets; Jul 5th, 2009 at 8:17 pm.
Also, please put your code in [code][/code] tags next time so that the indentation is kept, as it's a crucial part of Python.
Other than that, I'm still confused what you are having a problem with. Maybe the removing of the number from the whole square has to do with a different bit of your code - say if you're calling this eliminate function in a loop somewhere else which is causing the error.
Please just try to rephrase your question to be a little bit clearer, thanks.
Other than that, I'm still confused what you are having a problem with. Maybe the removing of the number from the whole square has to do with a different bit of your code - say if you're calling this eliminate function in a loop somewhere else which is causing the error.
Please just try to rephrase your question to be a little bit clearer, thanks.
Last edited by shadwickman; Jul 6th, 2009 at 5:05 pm.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
python Syntax (Toggle Plain Text)
def eliminate( grid, square, value ): row = square[ 0 ] col = square[ 1 ] poss = grid[ row ][ col ] if len( poss ) > 1: poss.remove( value ) grid[ row ][ col ] = poss # if len( poss ) == 1: # fixValue( grid, square, poss[ 0 ] )
And can you do a printout of what the list 'grid' looks like? That might be able to help, just one grid though. Not every one.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Dec 2006
Posts: 1,001
Reputation:
Solved Threads: 284
•
•
•
•
when I try to remove a single appearance of a number from a square, the number is removed from all the squares in the grid
Python Syntax (Toggle Plain Text)
##--- This declares a 3X5 list and prints the memory address grid = [] for j in range(0, 3): grid.append([ [] for x in range(5) ]) print grid for j in range(0, 3): for k in range(0, 5): print id(grid[j][k])
Python Syntax (Toggle Plain Text)
##--- declare a 1X3 list for testing grid = [] sub = [] for j in range(0, 3): grid.append(sub) print grid for j in range(0, 3): print id(grid[j]) ## they are all the same
Last edited by woooee; Jul 6th, 2009 at 8:14 pm.
Linux counter #99383
Ah, good point, I hadn't thought of that. This illustrates what woooee means:
They both refer to the same list because of the way they were declared (
python Syntax (Toggle Plain Text)
>>> a = b = [] >>> a.append('hi') >>> a.append('world!') >>> a ['hi', 'world!'] >>> b.remove('hi') >>> b ['world!'] >>> a ['world!'] >>> id(a) == id(b) # same address in memory True
a = b = [] ), so both names are actually for only one list. Last edited by shadwickman; Jul 6th, 2009 at 7:59 pm.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
Thanks for the replies guys. As you mentioned the problem was that all the elements in the 'grid' list had the same id, because instead of adding the numbers 1-9 to a list and then in the 'grid' list, I added a list that contained the numbers 1-9, e.g. tmp = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Thanks a lot for your help
Thanks a lot for your help
![]() |
Similar Threads
- First Post/Sudoku Solver problems (Java)
- Comments/Critiques Wanted on C++ OO solution to a sudoku solver (C++)
- Sudoku Solver (C++)
- recursive backtracking (sudoku solver) (C++)
- Sudoku Solver Problems Pt. 2 - Solve method issues (Java)
- recursive sudoku solver (asap) (C++)
- line print out for sudoku solver (Python)
Other Threads in the Python Forum
- Previous Thread: How to delete lines from a file we dont want ??
- Next Thread: Accessing system resources
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework ideas import inches input java launcher library line lines linux list lists loop mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






