sudoku solver problem

Thread Solved

Join Date: Jul 2009
Posts: 188
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 45
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Junior Poster

sudoku solver problem

 
0
  #1
Jul 5th, 2009
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...
Last edited by masterofpuppets; Jul 5th, 2009 at 8:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: sudoku solver problem

 
0
  #2
Jul 6th, 2009
>>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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 188
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 45
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Junior Poster

Re: sudoku solver problem

 
0
  #3
Jul 6th, 2009
sorry about that I'm still new to the website. The code is written in Python
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: sudoku solver problem

 
0
  #4
Jul 6th, 2009
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.
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 895
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 143
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: sudoku solver problem

 
0
  #5
Jul 6th, 2009
  1. def eliminate( grid, square, value ):
  2. row = square[ 0 ]
  3. col = square[ 1 ]
  4. poss = grid[ row ][ col ]
  5. if len( poss ) > 1:
  6. poss.remove( value )
  7. grid[ row ][ col ] = poss
  8. # if len( poss ) == 1:
  9. # fixValue( grid, square, poss[ 0 ] )
I think i got the indentation right

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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,001
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 284
woooee woooee is offline Offline
Veteran Poster

Re: sudoku solver problem

 
0
  #6
Jul 6th, 2009
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.
  1. ##--- This declares a 3X5 list and prints the memory address
  2. grid = []
  3. for j in range(0, 3):
  4. grid.append([ [] for x in range(5) ])
  5. print grid
  6.  
  7. for j in range(0, 3):
  8. for k in range(0, 5):
  9. print id(grid[j][k])
Edit: Based on shadwickman's post, you probably did something like
  1. ##--- declare a 1X3 list for testing
  2. grid = []
  3. sub = []
  4. for j in range(0, 3):
  5. grid.append(sub)
  6. print grid
  7.  
  8. for j in range(0, 3):
  9. print id(grid[j]) ## they are all the same
Last edited by woooee; Jul 6th, 2009 at 8:14 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: sudoku solver problem

 
0
  #7
Jul 6th, 2009
Ah, good point, I hadn't thought of that. This illustrates what woooee means:
  1. >>> a = b = []
  2. >>> a.append('hi')
  3. >>> a.append('world!')
  4. >>> a
  5. ['hi', 'world!']
  6. >>> b.remove('hi')
  7. >>> b
  8. ['world!']
  9. >>> a
  10. ['world!']
  11. >>> id(a) == id(b) # same address in memory
  12. 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.
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 188
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 45
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Junior Poster

Re: sudoku solver problem

 
0
  #8
Jul 6th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC