-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox
205 lines (168 loc) · 5.12 KB
/
sandbox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import copy
# delete this after debugging
def prettyPrint(L):
for row in L:
print(row)
def readFile(path):
with open(path, "rt") as f:
return f.read()
def boardTo2DList(board):
L = []
for line in board.splitlines():
M = line.split(' ')
L.append(M)
return L
def change2DListToTextBoard(board, rows, cols):
textBoard = ''
textRows = []
for row in range(rows):
textRow = ' '.join(board[row])
textRows.append(textRow)
textBoard = '\n'.join(textRows)
return textBoard
boardA = readFile('tp-starter-files/boards/easy-01.png.txt')
boardA = boardTo2DList(boardA)
# prettyPrint(boardA)
boardAInText = change2DListToTextBoard(boardA, 9, 9)
# print(boardAInText)
"""
boardA = boardTo2DList(boardA)
boardASol = readFile('tp-starter-files/solutions/easy-01-solution.png-solution.txt')
boardASol = boardTo2DList(boardASol)
"""
# START OF BACKTRACKING CODE
# adapted from solveMiniSudoku and isLegalSudoku
def solveSudoku(board):
board = copy.deepcopy(board)
if findNextEmptyCell(board) == None:
return board
else:
row, col = findNextEmptyCell(board)
for i in range(1,10):
board[row][col] = str(i)
if isBoardLegal(board):
# print(board)
solution = solveSudoku(board)
if solution != None:
return solution
board[row][col] = '0'
return None
def findNextEmptyCell(board):
rows, cols = len(board), len(board[0])
for row in range(rows):
for col in range(cols):
if board[row][col] == '0':
return row, col
return None
def isBoardLegal(board):
rows, cols = len(board), len(board[0])
for row in range(rows):
if not isLegalRow(board, row):
return False
for col in range(cols):
if not isLegalCol(board, col):
return False
blocks = rows
for block in range(blocks):
if not isLegalBlock(board, block):
return False
return True
def areLegalValues(L):
seen = set()
for value in L:
# value in not duplicate
if value != '0' and value in seen:
return False
seen.add(value)
return True
def isLegalRow(board, row):
return areLegalValues(board[row])
def isLegalCol(board, col):
rows = len(board)
values = [board[row][col] for row in range(rows)]
return areLegalValues(values)
def isLegalBlock(board, block):
n = len(board)
blockSize = round(n**0.5)
startRow = block // blockSize * blockSize
startCol = block % blockSize * blockSize
values = []
for drow in range(blockSize):
for dcol in range(blockSize):
row, col = startRow + drow, startCol + dcol
values.append(board[row][col])
return areLegalValues(values)
# END OF COPY
"""
print(boardA == boardASol) # false
solToBoardA = solveSudoku(boardA)
print(boardA == boardASol) # false
print(solToBoardA == boardASol) # true
"""
class Hello():
def __init__(self, num):
self.identity = num
self.bool = True
H = Hello(1)
Hi = copy.copy(H)
Hey = copy.deepcopy(Hi)
Hi.identity = 2
Hi.bool = False
Hey.identity = 3
# print(H.identity, Hi.identity, H.bool, Hey.identity)
def hint2(rows, cols): # finish this.
import itertools
# organize all the regions into a list
row1 = []
L = [row1, ]
for row in range(rows):
for col in range(cols):
pass
L = [ '0', '1', '2', '3', '4']
# loop through regions
for region in L:
for N in range(2,6):
for M in itertools.combinations(L, N):
# combine all the cells legals into one
sharedLegals = set()
for cell in M:
# add legals to general
sharedLegals.add(cell)
print(sharedLegals)
if len(sharedLegals) == N:
print('True')
return True
# for combination of certain cells of size N
# want to see if they have a combination of N legals
# if N = 2, and two cells have 5, 6 and 5, 6 as legals
# highlight the cells
# check each cell's legals
# add to cell
# hint2(4,2)
# find all the regions (row, col, block)
def findLegalObjectsinRow(board, row):
return board[row]
def findLegalObjectsinCol(board, col):
rows = len(board)
values = [board[row][col] for row in range(rows)]
return values
def findLegalObjectsinBlock(board, block):
blockSize = 3
startRow = block // blockSize * blockSize
startCol = block % blockSize * blockSize
values = []
for drow in range(blockSize):
for dcol in range(blockSize):
row, col = startRow + drow, startCol + dcol
values.append(board[row][col])
return values
# print(boardA)
# print(findLegalObjectsinBlock(boardA, 8))
class Hello():
def __init__(self, name):
self.name = name
L = [[Hello('anna'), Hello('elisa')], [Hello('steven')]]
M = copy.deepcopy(L)
L[0][1].name = 'helen'
print(M[0][1].name)
print(L is M)