| | |
soduko problem
Thread Solved
![]() |
•
•
Join Date: Jan 2007
Posts: 23
Reputation:
Solved Threads: 0
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 ?
thanx in advanceali
python Syntax (Toggle Plain Text)
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())
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 4
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.
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.
Request:
Response:
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.
Response:
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.
•
•
Join Date: Jan 2007
Posts: 23
Reputation:
Solved Threads: 0
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 :
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
python Syntax (Toggle Plain Text)
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]for j in range(81)]or r(a[:i]+m+a[i+1:])for m in`14**7*9`] r(raw_input())
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
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 4
python Syntax (Toggle Plain Text)
#solve sudoku by backtracking def solve_sudoku(board): #i is the first non-filled place, and the one we're going to try different values for i=board.find('0') #if i == -1, the puzzle is solved if i<0: print board #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 #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 # python has a nice feature called lazy evaluation. # if the first clause is true, the result of the other one is irrelevant for the output of "or". # therefore we use "a or b" to execute b only if a is false. 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)] #14**7*9 is 948721536, which contains all the digits from 1-9. #therefore "for m in `14**7*9` and the below are equivalent. #iterate through all the possible values for this square i. for m in "123456789": #lazy evaluation, again! #recursively call solve_sudoku itself with board[i] = m if this value of m is not on our exclusion list. [m in exclusion_list or solve_sudoku(board[:i]+m+board[i+1:])] #solve sudoku puzzle solve_sudoku(raw_input())
![]() |
Similar Threads
- Problem with Windows Update and WinXP (Web Browsers)
- Installing Windows 98 On VMware. Floppy problem (Windows 95 / 98 / Me)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Problem with T720 (Cellphones, PDAs and Handheld Devices)
- Connection Problems (Networking Hardware Configuration)
- Encoding (Unicode) problem in IE 6.0 (Web Browsers)
- .htaccess mod_rewrite problem (Linux Servers and Apache)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
Other Threads in the Python Forum
- Previous Thread: A Second Counter using Tkinter GUI
- Next Thread: Animated Images
| Thread Tools | Search this Thread |
alarm anydbm app assignment beginner bluetooth character cipher cmd conversion coordinates corners curves customdialog cx-freeze data decimals definedlines development directory events excel exe feet file float format function generator getvalue gnu halp handling homework ideas input ip itunes keycontrol leftmouse line linux list lists loan loop maintain maze millimeter module mouse number numbers output parsing path prime programming push py2exe pygame pymailer python queue random rational raw_input recursion recursive schedule screensaverloopinactive script searchingfile slicenotation sqlite ssh string strings sudokusolver text time tlapse tooltip tuple type ubuntu unicode url urllib urllib2 variable ventrilo vigenere web webservice wikipedia word wxpython xlib xlwt





