soduko problem

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2007
Posts: 23
Reputation: fonzali is an unknown quantity at this point 
Solved Threads: 0
fonzali fonzali is offline Offline
Newbie Poster

soduko problem

 
0
  #1
Aug 15th, 2007
hi guys , I am interested in writing a code to do the soduko ( or sudoku ) , I found this code on the internet which is supposed to be the shortest soduko solver but it is so short that I can not undrestand it . can any one help please ?
  1. def r(a): i=a.find('0') if i<0:print a [m in[(i-j)%9*(i/9^j/9)*(i/27^j/27|i%9/3^j%9/3)or a[j]forj in range(81)]or r(a[:i]+m+a[i+1:])for m in`14**7*9`]r(raw_input())
thanx in advanceali
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: soduko problem

 
0
  #2
Aug 15th, 2007
hi,
supposed to be the shortest soduko solver
yeah, intresting...
can any one help please ?
post the code with proper indentation.

kath.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 23
Reputation: fonzali is an unknown quantity at this point 
Solved Threads: 0
fonzali fonzali is offline Offline
Newbie Poster

Re: soduko problem

 
0
  #3
Aug 16th, 2007
this is how it was written on the internet , all in one line , I just paste and copied it
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: ffao is an unknown quantity at this point 
Solved Threads: 4
ffao ffao is offline Offline
Newbie Poster

Re: soduko problem

 
0
  #4
Aug 16th, 2007
There are basically two ways to solve a sudoku:

1) Solve it by logic, using simple rules such as the ones we, human beings, use, such as "if a square has only one possibility, than that number must be there". There are several solvers on the net that implement this kind of deductive solving, and reading the way they do it can probably give you an idea of what to do:

http://www.sudokusolver.co.uk/solvemethods.html
http://www.gwerdy.com/products/sudoku_solver/

2) Backtracking. Basically, using this method, you try every possible solution; if you reach a dead-end, you backtrack one square and try another number instead of the last number you tried. Googling for "sudoku solver" often reveals many pages about Donald Knuth's DLX algorithm, but I think something like that is better left to C.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 23
Reputation: fonzali is an unknown quantity at this point 
Solved Threads: 0
fonzali fonzali is offline Offline
Newbie Poster

Re: soduko problem

 
0
  #5
Aug 17th, 2007
thanks , those links could be helpful . I have not read them yet
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: soduko problem

 
0
  #6
Aug 17th, 2007
Request:
Originally Posted by katharnakh View Post
post the code with proper indentation.
Response:
Originally Posted by fonzali View Post
this is how it was written on the internet , all in one line , I just paste and copied it
fonzali, what kind of excuse is that? katharnakh made a reasonable request; getting the line breaks and indents right (and having the program work) is the obvious first step in figuring out the logic. You might even learn something in the process.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 23
Reputation: fonzali is an unknown quantity at this point 
Solved Threads: 0
fonzali fonzali is offline Offline
Newbie Poster

Re: soduko problem

 
0
  #7
Aug 17th, 2007
hi BearofNH , I did not mean to make excuses , the code looked pretty strange to me , this is how I figured it should be with proper indentation :
  1. def r(a):
  2. i=a.find('0')
  3. if i<0:
  4. print a
  5. [m in[(i-j)%9*(i/9^j/9)*(i/27^j/27|i%9/3^j%9/3)or a[j]for
  6. j in range(81)]or r(a[:i]+m+a[i+1:])for m in`14**7*9`]
  7. r(raw_input())
this is the best I could come up with and if it helps , this is how to run it :
echo 000010000301400860900500200700160000020805010000097004003004006048006907000080000 | python sudoku.py
one of the many things which I don't undrestand is the '14**7*9' , I hope I have been helpful . sorry for not doing this earlier ali
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: ffao is an unknown quantity at this point 
Solved Threads: 4
ffao ffao is offline Offline
Newbie Poster

Re: soduko problem

 
0
  #8
Aug 19th, 2007
  1. #solve sudoku by backtracking
  2. def solve_sudoku(board):
  3.  
  4. #i is the first non-filled place, and the one we're going to try different values for
  5. i=board.find('0')
  6.  
  7. #if i == -1, the puzzle is solved
  8. if i<0:
  9. print board
  10.  
  11. #iterate over all squares j; if the big test returns true, then j is in the same column, block or row as i and needs to be considered
  12. #if the test is true, the value of j square is appended, meaning the final result is a list of all the values the i square surely cannot have
  13.  
  14. # python has a nice feature called lazy evaluation.
  15. # if the first clause is true, the result of the other one is irrelevant for the output of "or".
  16. # therefore we use "a or b" to execute b only if a is false.
  17. exclusion_list = [(i-j)%9*(i/9^j/9)*(i/27^j/27|i%9/3^j%9/3)or a[j] for j in range(81)]
  18.  
  19. #14**7*9 is 948721536, which contains all the digits from 1-9.
  20. #therefore "for m in `14**7*9` and the below are equivalent.
  21. #iterate through all the possible values for this square i.
  22. for m in "123456789":
  23. #lazy evaluation, again!
  24. #recursively call solve_sudoku itself with board[i] = m if this value of m is not on our exclusion list.
  25. [m in exclusion_list or solve_sudoku(board[:i]+m+board[i+1:])]
  26.  
  27. #solve sudoku puzzle
  28. solve_sudoku(raw_input())
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 23
Reputation: fonzali is an unknown quantity at this point 
Solved Threads: 0
fonzali fonzali is offline Offline
Newbie Poster

Re: soduko problem

 
0
  #9
Aug 20th, 2007
hi ffao , thank you very much , now I undrestand what is going on . thanks again ali
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Python Forum


Views: 3085 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC