Guest User

Suduko

a guest
Mar 30th, 2020
145
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def complete(suduko):
  2.   for x in range(0,9):
  3.       for y in range(0,9):
  4.         if suduko[x][y]==0:
  5.           return False
  6.   return True
  7. def solve(suduko):
  8.   for dig in range(0,81):
  9.     x=dig//9
  10.     y=dig%9
  11.     if suduko[x][y]==0:
  12.       for val in range(1,10):
  13.         if not(val in suduko[x]):
  14.           if not val in (suduko[8][y],suduko[7][y],suduko[6][y],suduko[5][y],suduko[4][y],suduko[3][y],suduko[2][y],suduko[1][y],suduko[0][y]):
  15.             miniGrid=[]
  16.             if x<3:
  17.               if y<3:
  18.                 miniGrid=[suduko[dig][0:3] for dig in range(0,3)]
  19.               elif y<6:
  20.                 miniGrid=[suduko[dig][3:6] for dig in range(0,3)]
  21.               else:
  22.                 miniGrid=[suduko[dig][6:9] for dig in range(0,3)]
  23.             elif x<6:
  24.               if y<3:
  25.                 miniGrid=[suduko[dig][0:3] for dig in range(3,6)]
  26.               elif y<6:
  27.                 miniGrid=[suduko[dig][3:6] for dig in range(3,6)]
  28.               else:
  29.                 miniGrid=[suduko[dig][6:9] for dig in range(3,6)]
  30.             else:
  31.               if y<3:
  32.                 miniGrid=[suduko[dig][0:3] for dig in range(6,9)]
  33.               elif y<6:
  34.                 miniGrid=[suduko[dig][3:6] for dig in range(6,9)]
  35.               else:
  36.                 miniGrid=[suduko[dig][6:9] for dig in range(6,9)]
  37.             if not val in (miniGrid[0] + miniGrid[1] + miniGrid[2]):
  38.               suduko[x][y]=val
  39.               if complete(suduko):
  40.                 return True
  41.      
  42. suduko = []
  43. for x in range(9):
  44.   #differentiate each value by a space
  45.   #if no value, enter 0
  46.   x = list(map(int, input("Enter row: ").split()))
  47.   suduko.append(x)
  48. solved = solve(suduko)
  49. if solved:
  50.   print("Solved!!")
  51.   for x in suduko:
  52.    print(*x, sep=" ")
  53. else:
  54.   print("not working")
RAW Paste Data