What is the difference between two code blocks?
When I use this code block, my code doesn't work.
rows = [set()] * n
columns = [set()] * n
boxes3x3 = [set()] * n
But, when I used this block instead, my code works.
rows = [set() for _ in range(n)]
columns = [set() for _ in range(n)]
boxes3x3 = [set() for _ in range(n)]
Could you tell the difference between these blocks? How does it affect my code?
#Solution #2
# "r" stands for "row"
# "c" stands for "column"
n = 9
# vvv ..:: Not Works Here ::.. vvv #
#What are the wrong things here?
#rows = [set()] * n
#columns = [set()] * n
#boxes3x3 = [set()] * n
# ^^^ ..:: Not Works Here ::.. ^^^ #
# vvv ..:: Works Here ::.. vvv #
#How does it work here?
rows = [set() for _ in range(n)]
columns = [set() for _ in range(n)]
boxes3x3 = [set() for _ in range(n)]
# ^^^ ..:: Works Here ::.. ^^^ #
for r in range(n):
for c in range(n):
print(rows)
cell = board[r][c]
if(cell == '.'):
continue
if(cell in rows[r]):
return False
rows[r].add(cell)
if(cell in columns[c]):
return False
columns[c].add(cell)
boxInd = (r // 3) * 3 + c // 3
if(cell in boxes3x3[boxInd]):
return False
boxes3x3[boxInd].add(cell)
return True