943,512 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 5328
  • Python RSS
Aug 15th, 2007
0

soduko problem

Expand Post »
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 ?
python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
fonzali is offline Offline
25 posts
since Jan 2007
Aug 15th, 2007
0

Re: soduko problem

hi,
Quote ...
supposed to be the shortest soduko solver
yeah, intresting...
Quote ...
can any one help please ?
post the code with proper indentation.

kath.
Reputation Points: 19
Solved Threads: 34
Posting Whiz in Training
katharnakh is offline Offline
237 posts
since Jan 2006
Aug 16th, 2007
0

Re: soduko problem

this is how it was written on the internet , all in one line , I just paste and copied it
Reputation Points: 10
Solved Threads: 0
Light Poster
fonzali is offline Offline
25 posts
since Jan 2007
Aug 16th, 2007
0

Re: soduko problem

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.
Reputation Points: 20
Solved Threads: 5
Newbie Poster
ffao is offline Offline
15 posts
since May 2007
Aug 17th, 2007
0

Re: soduko problem

thanks , those links could be helpful . I have not read them yet
Reputation Points: 10
Solved Threads: 0
Light Poster
fonzali is offline Offline
25 posts
since Jan 2007
Aug 17th, 2007
0

Re: soduko problem

Request:
Click to Expand / Collapse  Quote originally posted by katharnakh ...
post the code with proper indentation.
Response:
Click to Expand / Collapse  Quote originally posted by fonzali ...
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.
Reputation Points: 94
Solved Threads: 48
Posting Whiz
BearofNH is offline Offline
321 posts
since May 2007
Aug 17th, 2007
0

Re: soduko problem

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 :
python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
fonzali is offline Offline
25 posts
since Jan 2007
Aug 19th, 2007
0

Re: soduko problem

python Syntax (Toggle Plain Text)
  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())
Reputation Points: 20
Solved Threads: 5
Newbie Poster
ffao is offline Offline
15 posts
since May 2007
Aug 20th, 2007
0

Re: soduko problem

hi ffao , thank you very much , now I undrestand what is going on . thanks again ali
Reputation Points: 10
Solved Threads: 0
Light Poster
fonzali is offline Offline
25 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: ceaser cypher
Next Thread in Python Forum Timeline: Animated Images





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC