Advertisement
franji

Suduko arr(tuple) NotImplemented

Mar 7th, 2018
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. game = """
  2. _ _ 4  _ _ _  _ 5 7
  3. 2 _ _  _ _ _  8 _ _
  4. 3 9 _  5 _ _  _ _ 6
  5.  
  6. _ 1 _  4 7 5  9 _ _
  7. _ _ _  _ 3 _  _ _ _
  8. _ _ 5  2 1 8  _ 3 _
  9.  
  10. 7 _ _  _ _ 9  _ 4 8
  11. _ _ 9  _ _ _  _ _ 3
  12. 1 8 _  _ _ _  2 _ _
  13. """
  14.  
  15. game1='''
  16. _ _ _ _ 6 1 _ _ 5
  17. _ 3 _ _ _ _ _ _ 9
  18. _ 4 _ _ _ 3 1 _ _
  19.  
  20. 7 8 _ 3 _ _ _ 5 2
  21. _ _ 2 _ _ _ 9 _ _
  22. 5 9 _ _ _ 8 _ 1 6
  23.  
  24. _ _ 7 1 _ _ _ 9 _
  25. 6 _ _ _ _ _ _ 7 _
  26. 9 _ _ 6 2 _ _ _ _
  27. '''
  28.  
  29.  
  30. game_test='''
  31. _ 2 3 4 5 6 7 8 9
  32. _ 5 _ _ _ _ _ _ _
  33. _ 6 _ _ _ _ _ _ _
  34.  
  35. _ 3 _ _ _ _ _ _ _
  36. _ 4 _ _ _ _ _ _ _
  37. _ 7 _ _ _ _ _ _ _
  38.  
  39. _ 8 _ _ _ _ 2 3 4
  40. _ 9 _ _ _ _ 5 6 7
  41. _ _ _ _ _ _ 8 9 _
  42. '''
  43.  
  44.  
  45.  
  46. def BoardFromInput(game):
  47.   res = [[]]
  48.   for ch in game:
  49.     item = None
  50.     if ch.isdigit():
  51.       item = [True, int(ch)]
  52.     elif ch == '_':
  53.       item = [False, None]
  54.    
  55.     if item:
  56.       if len(res[-1]) >= 9:
  57.         res.append([])
  58.       res[-1].append(item)
  59.   if len(res) != 9 or len(res[-1]) != 9:
  60.     print "ERROR - illegal Suduko input"
  61.   return res
  62.  
  63.  
  64. def PrintBoard(board):
  65.   for (li, line) in enumerate(board):
  66.     if li % 3 == 0:
  67.        print "-" * 47
  68.     for (ci, (isset, num)) in enumerate(line):
  69.       if ci % 3 == 0:
  70.         print " | ",
  71.       if num:
  72.         print num, " ",
  73.       else:
  74.         print "_", " ",
  75.     print
  76.   print "-" * 47
  77.  
  78.  
  79. def InLine(n, board, line):
  80.     raise NotImplementedError
  81.  
  82. def InCol(n, board, col):
  83.     raise NotImplementedError
  84.  
  85. def InSq(n, board, line, col):
  86.     raise NotImplementedError
  87.  
  88.  
  89. def _test():
  90.   board = BoardFromInput(game_test)
  91.   assert InLine(2, board, 0)
  92.   assert not InLine(1, board, 0)
  93.   assert InCol(2, board, 1)
  94.   assert not InCol(1, board, 1)
  95.   assert InSq(2, board, 8, 8)
  96.   assert not InSq(1, board, 8, 8)
  97.  
  98.  
  99. def validate_game(game):
  100.   board = BoardFromInput(game)
  101.   for l in xrange(9):
  102.     for c in xrange(9):
  103.       if not board[l][c][0]:
  104.         continue  # skip the free cells
  105.       v = board[l][c][1]
  106.       board[l][c][0] = False
  107.       board[l][c][1] = None
  108.       #print "line=", l , "col=", c, "num=", v
  109.       assert not InLine(v, board, l)
  110.       assert not InCol(v, board, c)
  111.       assert not InSq(v, board, l, c)
  112.       # restore
  113.       board[l][c][0] = True
  114.       board[l][c][1] = v
  115.  
  116.  
  117.  
  118. def Solve(board, i):
  119.   #PrintBoard(board)
  120.   #raw_input(">>>")
  121.   if i >= 9 * 9:
  122.     return True
  123.   line = i / 9
  124.   col = i % 9
  125.   if board[line][col][0]:
  126.     return Solve(board, i + 1)
  127.   for n in range(1,10):
  128.     board[line][col][1] = None
  129.     line_bad = InLine(n, board, line)
  130.     row_bad = InCol(n, board, col)
  131.     sq_bad = InSq(n, board, line, col)
  132.     #print "line_bad", line_bad, "row", row_bad, "sq", sq_bad
  133.     if line_bad or row_bad or sq_bad:
  134.       continue
  135.     board[line][col][1] = n
  136.     if Solve(board, i + 1):
  137.       return True
  138.     board[line][col][1] = None
  139.   return False
  140.  
  141.  
  142.  
  143. validate_game(game1)
  144. board = BoardFromInput(game1)
  145. print "SUDUKO:"
  146. PrintBoard(board)
  147.  
  148. if Solve(board, 0):
  149.   print "SOLUTION:"
  150.   PrintBoard(board)
  151. else:
  152.   print "NO SOUTION"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement