Advertisement
franji

suduku obj

Mar 14th, 2018
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. import sys
  2. import unittest
  3.  
  4. game = """
  5. _ _ 4  _ _ _  _ 5 7
  6. 2 _ _  _ _ _  8 _ _
  7. 3 9 _  5 _ _  _ _ 6
  8.  
  9. _ 1 _  4 7 5  9 _ _
  10. _ _ _  _ 3 _  _ _ _
  11. _ _ 5  2 1 8  _ 3 _
  12.  
  13. 7 _ _  _ _ 9  _ 4 8
  14. _ _ 9  _ _ _  _ _ 3
  15. 1 8 _  _ _ _  2 _ _
  16. """
  17.  
  18. class FixedCell(object):
  19.   """An item on the Sudoku board. Can be fixed or free.
  20.  """
  21.   pass
  22.  
  23. class FreeCell(object):
  24.   """An item on the Sudoku board. Can be fixed or free.
  25.  """
  26.   pass
  27.  
  28. def InLine(n, board, line):
  29.   raise NotImplemetedError()
  30.  
  31.  
  32. def InCol(n, board, col):
  33.   raise NotImplemetedError()
  34.  
  35.  
  36. def InSq(n, board, line, col):
  37.   raise NotImplemetedError()
  38.  
  39.  
  40. def BoardFromInput(game):
  41.     """Parse string into a list of lists of cells"""
  42.     res = [[]]
  43.     for ch in game:
  44.         item = None
  45.         if ch.isdigit():
  46.             item = FixedCell(int(ch))
  47.         elif ch == '_':
  48.             item = FreeCell()
  49.         if item:
  50.             if len(res[-1]) >= 9:
  51.                 res.append([])
  52.             res[-1].append(item)
  53.     if len(res) != 9 or len(res[-1]) != 9:
  54.         print "ERROR - illegal Sudoku input"
  55.     return res
  56.  
  57.  
  58. def PrintBoard(board):
  59.     for (li, line) in enumerate(board):
  60.         if li % 3 == 0:
  61.             print "-" * 47
  62.         for (ci, cell) in enumerate(line):
  63.             if ci % 3 == 0:
  64.                 print " | ",
  65.             if cell.val():
  66.                 print cell.val(), " ",
  67.             else:
  68.                 print "_", " ",
  69.         print
  70.     print "-" * 47
  71.  
  72.  
  73. def Solve(board, i):
  74.     # Yo can add the following lines to see advance step by step
  75.     #PrintBoard(board)
  76.     #raw_input("??>")
  77.     if i >= 9 * 9:
  78.         return True
  79.     line = i / 9
  80.     col = i % 9
  81.     if isinstance(board[line][col], FixedCell):
  82.         return Solve(board, i + 1)
  83.     for n in range(1,10):
  84.         board[line][col].clear_val()
  85.         line_bad = InLine(n, board, line)
  86.         row_bad = InCol(n, board, col)
  87.         sq_bad = InSq(n, board, line, col)
  88.         if line_bad or row_bad or sq_bad:
  89.             continue
  90.         board[line][col].set_val(n)
  91.         if Solve(board, i + 1):
  92.             return True
  93.         board[line][col].clear_val()
  94.     return False
  95.  
  96.  
  97. def main(argv):
  98.   board = BoardFromInput(game)
  99.   print "SUDOKU:"
  100.   PrintBoard(board)
  101.  
  102.   if Solve(board, 0):
  103.     print "SOLUTION:"
  104.     PrintBoard(board)
  105.   else:
  106.     print "NO SOUTION"
  107.  
  108.  
  109. if __name__ == "__main__":
  110.     sys.exit(main(sys.argv))
  111.  
  112.  
  113. class TestSudoku(unittest.TestCase):
  114.  
  115.   def setUp(self):
  116.     game_test='''
  117. _ 2 3 4 5 6 7 8 9
  118. _ 5 _ _ _ _ _ _ _
  119. _ 6 _ _ _ _ _ _ _
  120.  
  121. _ 3 _ _ _ _ _ _ _
  122. _ 4 _ _ _ _ _ _ _
  123. _ 7 _ _ _ _ _ _ _
  124.  
  125. _ 8 _ _ _ _ 2 3 4
  126. _ 9 _ _ _ _ 5 6 7
  127. _ _ _ _ _ _ 8 9 _
  128. '''
  129.     self.board = BoardFromInput(game_test)
  130.  
  131.   def test_InLine(self):
  132.     self.assertTrue(InLine(2, self.board, 0))
  133.     self.assertFalse(InLine(1, self.board, 0))
  134.  
  135.   def test_InCol(self):
  136.     self.assertTrue(InCol(2, self.board, 1))
  137.     self.assertFalse(InCol(1, self.board, 1))
  138.  
  139.   def test_InSq(self):
  140.     self.assertTrue(InSq(2, self.board, 8, 8))
  141.     self.assertFalse(InSq(1, self.board, 8, 8))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement