• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jun 16th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def sudoku(puzzle):
2.
3.     # copy the puzzle to be able to save original positions that are changeable
4.     pc = puzzle.copy()
5.
6.     puzzle = pns(puzzle)
7.
8.     return puzzle
9.
10.
11.
12. def pns(puzzle):
13.     for y in range(len(puzzle)):
14.         for x in range(len(puzzle[0])):
15.
16.             pn = set(range(1,len(puzzle) + 1))
17.
18.             for row_idx in range(len(puzzle)):
19.                 for col_idx in range(len(puzzle[row_idx])):
20.
21.                     skip_bool = True
22.                     if puzzle[y][x] != 0 and type(puzzle[y][x]) == type(1):
23.                         skip_bool = False
24.
25.                     if row_idx == y and col_idx != x and type(puzzle[row_idx][col_idx]) != type([0]):
26.                         if puzzle[row_idx][col_idx] in pn:
27.                             pn.remove(puzzle[row_idx][col_idx])
28.
29.                     if col_idx == x and row_idx != y and type(puzzle[row_idx][col_idx]) !=  type([0]):
30.                         if puzzle[row_idx][col_idx] in pn:
31.                             pn.remove(puzzle[row_idx][col_idx])
32.             pn = list(pn)
33.
34.             if len(pn) == 1:
35.                 puzzle[y][x] = pn[0]
36.
37.             elif len(pn) == 0:
38.                 puzzle[y][x] = False
39.
40.             elif skip_bool:
41.                 puzzle[y][x] = pn
42.
43.     return puzzle
44.
45. print(pc)
46. print(puzzle)
47.
48. [5, 3, 0, 0, 7, 0, 0, 0, 0]
49. [6, 0, 0, 1, 9, 5, 0, 0, 0]
50. [0, 9, 8, 0, 0, 0, 0, 6, 0]
51. [8, 0, 0, 0, 6, 0, 0, 0, 3]
52. [4, 0, 0, 8, 0, 3, 0, 0, 1]
53. [7, 0, 0, 0, 2, 0, 0, 0, 6]
54. [0, 6, 0, 0, 0, 0, 2, 8, 0]
55. [0, 0, 0, 4, 1, 9, 0, 0, 5]
56. [0, 0, 0, 0, 8, 0, 0, 7, 9]
57.
58. [5, 3, [1, 2, 4, 6, 9], [2, 6, 9], 7, [1, 2, 4, 6, 8], [1, 4, 6, 8, 9], [1, 2, 4, 9], [2, 4, 8]]
59. [6, [2, 4, 7, 8], [2, 3, 4, 7], 1, 9, 5, [3, 4, 7, 8], [2, 3, 4], [2, 4, 7, 8]]
60. [[1, 2, 3], 9, 8, [2, 3, 5, 7], [3, 4, 5], [1, 2, 4, 7], [1, 3, 4, 5, 7], 6, [2, 4, 7]]
61. [8, [1, 2, 4, 5, 7], [1, 2, 4, 5, 7, 9], [2, 5, 7, 9], 6, [1, 2, 4, 7], [1, 4, 5, 7, 9], [1, 2, 4, 5, 9], 3]
62. [4, [2, 5, 7], [2, 5, 6, 7, 9], 8, 5, 3, [6, 7, 9], [2, 9], 1]
63. [7, [1, 4, 5, 8], [1, 3, 4, 5, 9], [3, 5, 9], 2, [1, 4, 8], [1, 3, 4, 5, 8, 9], [1, 3, 4, 5, 9], 6]
64. [[1, 3, 9], 6, [1, 3, 4, 5, 7, 9], [3, 5, 7, 9], [3, 4], [1, 4, 7], 2, 8, [4, 7]]
65. [[2, 3], [2, 7, 8], [2, 3, 6, 7], 4, 1, 9, [3, 6, 7, 8], [2, 3], 5]
66. [[1, 2, 3], [1, 2, 4, 5], [1, 2, 3, 4, 5, 6], [2, 3, 5, 6], 8, [1, 2, 4, 6], [1, 3, 4, 5, 6], 7, 9]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top