Implementing Sudoku algorithm in Python

From , 5 Years ago, written in Python, viewed 201 times.
URL https://pastebin.vip/view/5e69fda3
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Created on 2012-10-5
  4. @author: Administrator
  5. '''
  6. from collections import defaultdict
  7. import itertools
  8.  
  9. a = [
  10.         [ 0, 7, 0, 0, 0, 0, 0, 0, 0], #0
  11.         [ 5, 0, 3, 0, 0, 6, 0, 0, 0], #1
  12.         [ 0, 6, 2, 0, 8, 0, 7, 0, 0], #2
  13.         #
  14.         [ 0, 0, 0, 3, 0, 2, 0, 5, 0], #3
  15.         [ 0, 0, 4, 0, 1, 0, 3, 0, 0], #4
  16.         [ 0, 2, 0, 9, 0, 5, 0, 0, 0], #5
  17.         #
  18.         [ 0, 0, 1, 0, 3, 0, 5, 9, 0], #6
  19.         [ 0, 0, 0, 4, 0, 0, 6, 0, 3], #7
  20.         [ 0, 0, 0, 0, 0, 0, 0, 2, 0], #8
  21. #        0, 1, 2, 3,|4, 5, 6,|7, 8
  22.      ]
  23. #a = [
  24. #        [0, 0, 0, 0, 0, 0, 0, 0, 0], #0
  25. #        [0, 0, 0, 0, 0, 0, 0, 0, 0], #1
  26. #        [0, 0, 0, 0, 0, 0, 0, 0, 0], #2
  27. #        #
  28. #        [0, 0, 0, 0, 0, 0, 0, 0, 0], #3
  29. #        [0, 0, 0, 0, 0, 0, 0, 0, 0], #4
  30. #        [0, 0, 0, 0, 0, 0, 0, 0, 0], #5
  31. #        #
  32. #        [0, 0, 0, 0, 0, 0, 0, 0, 0], #6
  33. #        [0, 0, 0, 0, 0, 0, 0, 0, 0], #7
  34. #        [0, 0, 0, 0, 0, 0, 0, 0, 0], #8
  35. ##        0, 1, 2, 3,|4, 5, 6,|7, 8
  36. #     ]
  37.  
  38. exists_d = dict((((h_idx, y_idx), v) for h_idx, y in enumerate(a) for y_idx , v in enumerate(y)  if v))
  39.  
  40. h_exist = defaultdict(dict)
  41. v_exist = defaultdict(dict)
  42.  
  43. for k, v in exists_d.items():
  44.     h_exist[k[ 0]][k[ 1]] = v
  45.     v_exist[k[ 1]][k[ 0]] = v
  46.  
  47.  
  48. aa = list(itertools.permutations(range(1, 10), 9))
  49.  
  50. h_d = {}
  51.  
  52. for hk, hv in h_exist.items():
  53.    
  54.     x = filter(lambda x:all((x[k] == v for k, v in hv.items())), aa)
  55.     x = filter(lambda x:all((x[vk] != v for vk , vv in v_exist.items() for k, v in vv.items() if k != hk)), x)
  56. #    print x
  57.     h_d[hk] = x
  58.  
  59.  
  60. def test(x, y):
  61.     return all([y[i] not in [x_[i] for x_ in x] for i in range(len(y)) ])
  62.  
  63. def test2(x):
  64.     return len(set(x)) != 9
  65.  
  66. s = set(range(9))
  67.  
  68. sudokus = []
  69. for l0 in h_d[0 ]:
  70.    
  71.     for l1 in h_d[ 1]:
  72.         if not test((l0,), l1):
  73.             continue
  74.         for l2 in h_d[ 2]:
  75.             if not test((l0, l1), l2):
  76.                 continue
  77.            # 1,2,3行 进行验证
  78.             if test2([l0[ 0], l0[ 1], l0[ 2]
  79.                         , l1[ 0], l1[ 1], l1[ 2]
  80.                         , l2[ 0], l2[ 1], l2[ 2]
  81.                        ])  : continue          
  82.  
  83.             if test2([l0[ 3], l0[ 4], l0[ 5]
  84.                         , l1[ 3], l1[ 4], l1[ 5]
  85.                         , l2[ 3], l2[ 4], l2[ 5]
  86.                        ])  : continue          
  87.  
  88.             if test2([l0[ 6], l0[ 7], l0[ 8]
  89.                         , l1[ 6], l1[ 7], l1[ 8]
  90.                         , l2[ 6], l2[ 7], l2[ 8]
  91.                        ])  : continue          
  92.            
  93.             for l3 in h_d[ 3]:
  94.                 if not test((l0, l1, l2), l3):
  95.                     continue
  96.                
  97.                 for l4 in h_d[ 4]:
  98.                     if not test((l0, l1, l2, l3), l4):
  99.                         continue
  100.                    
  101.                     for l5 in h_d[ 5]:
  102.                         if not test((l0, l1, l2, l3, l4), l5):
  103.                             continue
  104.                        # 4,5,6行 进行验证
  105.                         if test2([l3[ 0], l3[ 1], l3[ 2]
  106.                                     , l4[ 0], l4[ 1], l4[ 2]
  107.                                     , l5[ 0], l5[ 1], l5[ 2]
  108.                                    ])  : continue          
  109.            
  110.                         if test2([l3[ 3], l3[ 4], l3[ 5]
  111.                                     , l4[ 3], l4[ 4], l4[ 5]
  112.                                     , l5[ 3], l5[ 4], l5[ 5]
  113.                                    ])  : continue          
  114.            
  115.                         if test2([l3[ 6], l3[ 7], l3[ 8]
  116.                                     , l4[ 6], l4[ 7], l4[ 8]
  117.                                     , l5[ 6], l5[ 7], l5[ 8]
  118.                                    ])  : continue          
  119.                        
  120.                         for l6 in h_d[ 6]:
  121.                             if not test((l0, l1, l2, l3, l4, l5,), l6):
  122.                                 continue
  123.                             for l7 in h_d[ 7]:
  124.                                 if not test((l0, l1, l2, l3, l4, l5, l6), l7):
  125.                                     continue
  126.                                 for l8 in h_d[ 8]:
  127.                                     if not test((l0, l1, l2, l3, l4, l5, l6, l7), l8):
  128.                                         continue
  129.                                    # 7,8,9行 进行验证
  130.                                     if test2([l6[ 0], l6[ 1], l6[ 2]
  131.                                                 , l7[0 ], l7[1 ], l7[2 ]
  132.                                                 , l8[0 ], l8[1 ], l8[2 ]
  133.                                                ])  : continue          
  134.                        
  135.                                     if test2([l6[ 3], l6[ 4], l6[ 5]
  136.                                                 , l7[3 ], l7[4 ], l7[5 ]
  137.                                                 , l8[3 ], l8[4 ], l8[5 ]
  138.                                                ])  : continue          
  139.                        
  140.                                     if test2([l6[ 6], l6[ 7], l6[ 8]
  141.                                                 , l7[6 ], l7[7 ], l7[8 ]
  142.                                                 , l8[6 ], l8[7 ], l8[8 ]
  143.                                                ])  : continue          
  144.                                    
  145.                                     print l0
  146.                                     print l1
  147.                                     print l2
  148.                                     print l3
  149.                                     print l4
  150.                                     print l5
  151.                                     print l6
  152.                                     print l7
  153.                                     print l8
  154.                                    
  155.                                     sudokus.append((l0, l1, l2, l3, l4, l5, l6, l7, l8))
  156.                                    
  157.                                    
  158.    
  159.    
  160.    
  161. #//python/5339

Reply to "Implementing Sudoku algorithm in Python"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website