Sudoku
Estimated time
60-90 minutes
Level of difficulty
Hard
Scenario
As you probably know, Sudoku is a number-placing puzzle played on a 9x9 board. The player has to fill the board in a very specific way:
- each row of the board must contain all digits from 0 to 9 (the order doesn't matter)
- each column of the board must contain all digits from 0 to 9 (again, the order doesn't matter)
- each of the nine 3x3 "tiles" (we will name them "sub-squares") of the table must contain all digits from 0 to 9.
Your task is to write a program which:
- reads 9 rows of the Sudoku, each containing 9 digits (check carefully if the data entered are valid)
- outputs
Yesif the Sudoku is valid, andNootherwise.
Test your code using the data we've provided.
Test data
Sample input:
295743861
431865927
876192543
387459216
612387495
549216738
763524189
928671354
154938672
Sample output
YesSample input:
195743862
431865927
876192543
387459216
612387495
549216738
763524189
928671354
254938671
Sample output:
No
CODE:
final_rows=[]
row_check=False
def check_input(row):
global row_check #otherwise row_check will be treated as local variable
row_check=False #so previous value is overwrite
try:
#checking length of input
if len(row)==9:
#checking if digit repeated or has zero
for num in row:
if row.count(num)!=1 or num=="0":
raise TypeError
#row=int(row)
final_rows.append(row)
row_check=True
return
else:
raise ValueError
except ValueError:
print(f"Enter valid data for {i+1} row")
except TypeError:
print(f"Repeating digits or 0 found in {i+1} row")
#checking repetition in columns
def column_check():
global result
column=[ ]
for j in range(len(final_rows)):
d=final_rows[j][i]
#print(i)
column.append(d)
#print(column)
for c in column:
if column.count(c)!=1:
result=False
break
def tile_check():
global result,start_vertical,start_horizontal
#resetting tiles after each 3x3
tiles=[ ]
#filling 3x3 numbers in tiles
#repeats filling of tiles with 3 nums (3 times vertically) then jumps vertically
for k in range(start_vertical,start_vertical+3):
#fills tiles horizontally with 3 nums
for l in range(start_horizontal,start_horizontal+3):
t=final_rows[k][l]
tiles.append(t)
#checking repition in 3x3 tile
for num in tiles:
if tiles.count(num)!=1:
result=False
return
#print(tiles)
start_horizontal+=3
#main program
#checking data input
for i in range(9):
row=input(f"Enter 9 digits for {i+1} row:")
check_input(row)
#print(row_check)
while not row_check:
row=input(f"Enter 9 digits for {i+1} row:")
check_input(row)
#print(final_rows)
column=[]
result=True
#checking repetition in columns
for i in range(len(final_rows)):
column_check()
if not result:
break
#print(result)
#checking tiles i.e 3x3 sub squares
start_horizontal=0
start_vertical=0
#only executed if column check passed
while result and start_vertical<=6:
#checking horizontally with vertical jumps after each check
for j in range(3):
tile_check()
if not result:
break
start_horizontal=0 #to jump horizontally
start_vertical+=3 #to jump vertically
if result:
print("Yes")
else:
print("No")
Comments
Post a Comment