Member Avatar for masterofpuppets

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...

>>If you need other pieces of the code I'll post them here...

Please don't. Post all your technical questions in one of the language boards. I would move this to the right place if I knew what computer language that code was written in.

Member Avatar for masterofpuppets

:) sorry about that I'm still new to the website. The code is written in Python :)

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.

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 ] )

I think i got the indentation right :P

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.

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

Generally that means that you are not declaring a list of independent lists but a list of references to the same list. Show us how you are creating "grid", and/or loop through and print id(this_sub_list) and see if the addresses are the same.

##---  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])

Edit: Based on shadwickman's post, you probably did something like

##--- 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

Ah, good point, I hadn't thought of that. This illustrates what woooee means:

>>> 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

They both refer to the same list because of the way they were declared ( a = b = [] ), so both names are actually for only one list.

Member Avatar for masterofpuppets

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.