sudoku solver problem

Reply

Join Date: Nov 2009
Posts: 3
Reputation: moazmizo is an unknown quantity at this point 
Solved Threads: 0
moazmizo moazmizo is offline Offline
Newbie Poster

sudoku solver problem

 
0
  #1
19 Days Ago
hi, i have to make a sudoku solver using python quickdraw,
i've started on it and this is what i got so far
here is the link to the assignment http://pages.cpsc.ucalgary.ca/~zongp...ents/A4/a4.pdf
  1. def solveRows():
  2. """eliminates solved numbers by row"""
  3. for x in range(0, 81, 9):
  4. row = puzzle[x : x+9]#loops through each row
  5. for space in row: #loops through each space
  6. if len(space) == 1: #space is solved
  7. for space2 in row: #loops through spaces again
  8. if len(space2) != 1: #space isnt already solved
  9. if space[0] in space2: #the solved number is a possibility
  10. del space2[space2.index(space[0])] #deletes the number as a possiblbitly
  11. def solveColumns():
  12. """eliminates solved numbers by column"""
  13. rows = []#splits up the puzzle into rows
  14. for x in range(0, 81, 9):
  15. rows.append(puzzle[x:x+9])
  16. for n in range(0, 9):
  17. column = [x[n] for x in rows] #loops through each column
  18. #the next part is taken from solveRows()
  19. for space in column: #loops through each space
  20. if len(space) == 1: #space is solved
  21. for space2 in column: #loops through spaces again
  22. if len(space2) != 1: #space isnt already solved
  23. if space[0] in space2: #the solved number is a possibility
  24. del space2[space2.index(space[0])] #deletes the number as a possiblbitly
  25.  
  26. def solveBoxes():
  27. """eliminates solved numbers by box"""
  28. rows = []#splits up the puzzle into rows
  29. for x in range(0, 81, 9):
  30. rows.append(puzzle[x:x+9])
  31. #this next part just splits the puzzle into boxes
  32. for x in range(0, 9, 3):
  33. for y in range(0, 9, 3):
  34. tempRows = rows[x:x+3]
  35. tempBox = [row[y:y+3] for row in tempRows]
  36. #the next part just formats the box
  37. #basically it was a list of lists of lists
  38. #and i need it as a list of lists
  39. box = []
  40. for row in tempBox:
  41. for space in row:
  42. box.append(space)
  43. #the next part is taken from solveRows()
  44. for space in box: #loops through each space
  45. if len(space) == 1: #space is solved
  46. for space2 in box: #loops through spaces again
  47. if len(space2) != 1: #space isnt already solved
  48. if space[0] in space2: #the solved number is a possibility
  49. del space2[space2.index(space[0])] #deletes the number as a possiblbitly
  50.  
  51. def isSolved():
  52. """Checks to see if the puzzle is solved"""
  53. for x in puzzle:
  54. if len(x) != 1:
  55. return False
  56. else:
  57. return True
  58.  
  59. def main():
  60. while True:
  61. temp = puzzle #used to see when puzzle is solved
  62. solveBoxes()
  63. solveRows()
  64. solveColumns()
  65. if isSolved():#puzzle is solved
  66. return
  67.  
  68. def display():
  69. """ displays the puzzle"""
  70. copy = puzzle #copy of the puzzle
  71. copy = map(str, [x[0] for x in copy]) #makes the ints strs so i can use S.join()
  72. #next part just displays the puzzle all nice and pretty
  73. for x in range(0, 9):
  74. print ', '.join(copy[x:x+9])
  75.  
  76. def getInput():
  77. """gets a puzzle to be solved from the user"""
  78. puzzle = []
  79. for x in range(1, 10): #1 for each line of the puzzle
  80. puzzle.append(raw_input('enter one line of the puzzle '))
  81. puzStr = '\n'.join(puzzle)
  82. puzzle = []#a soon to be matrix holding possibilities for each space
  83. for letter in puzStr:
  84. if letter == 'x':
  85. puzzle.append(range(1, 10)) #adds all possibilities if square is blank
  86. elif letter == '\n' or letter ==' ':
  87. continue
  88. else:
  89. puzzle.append([int(letter)]) #adds the already given number
  90. return puzzle
  91.  
  92. print """This program will solve some suDoku puzzles
  93. If it can't it will just hang indefinately so give it a little
  94. while then if nothing happens assume that either you entered
  95. the puzzle incorrectly, it is not solvable, or this program
  96. can't solve it.
  97.  
  98. Begin by entering the puzzle in line by line.
  99. Represent empty spaces by an 'x'. So a line
  100. migh look like '6x12x897x'. This program as of now
  101. will not catch your errors so try not to make mistakes.
  102. You can but do not have to add any spaces when entering the puzzle.
  103.  
  104. Enjoy!
  105. """
  106. puzzle = getInput()
  107. main()
  108. print 'The answer is:'
  109. print
  110. display()
i don't know what to do next,i'[ve tried putting like
6x12x897x, x8x6x5x2x, x54xxxxx1, 4xx796xxx, x9xxxxx1x
xxx182xx7, 3xxxxx75x, x7x3x9x4x, x285x41x3
didn't work and it said errno #22 close failed
so, am i doing the assignment right
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC