Complete the code.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Complete the code. 

 

Write a function valid Solution/ValidateSolution/valid_solution ()
that accepts a 2D array representing a Sudoku board, and returns true
if it is a valid solution, or false otherwise. The cells of the sudoku
board may also contain 0's, which will represent empty cells.
Boards containing one or more zeroes are considered to be invalid solutions.
The board is always 9 cells by 9 cells, and every cell only contains integers
from 0 to 9.
(More info at: http://en.wikipedia.org/wiki/Sudoku)
#Using dict/hash-table
from collections import defaultdict
def valid_solution_hashtable(board):
for i in range(len (board)):
dict_row=defaultdict(int)
dict_col=defaultdict(int)
forj in range(len (board[0])):
value_row = board [i][j]
value_col=board [j][i]
if not value_row or value_col== 0:
return False
if value_row in dict_row:
return false
else:
dict_row[value_row] += 1
if value_col in dict_col:
return False
dict_col[value_col] += 1
else:
for i in range(3):
forj in range(3):
grid_add = 0
fork in range(3):
for l in range(3):
grid_add += board [i*3 + k][j* 3+1]
if grid_add != 45:
return False
return True
# Without hash-table/dict
def valid_solution (board):
correct = [1, 2, 3, 4, 5, 6, 7, 8, 9]
#check rows
for row in board:
if sorted (row) != correct:
return false
# check columns
for column in zip (*board):
if sorted (column) != correct:
return false
#check regions
for i in range(3):
for j in range(3):
region = []
for line in board [i*3:(i+1)*3]:
region += line[j*3:(j+1)*3]
if sorted (region) != correct:
return False
# if everything correct
return True
# Using set
def valid_solution_set(board):
valid = set(range(1,10))
for row in board:
if set(row)!= valid:
return false
for col in [[row[i] for row in board] for i in range(9)]:
if set(col) != valid:
return false
for x in range(3):
fory in range(3):
Transcribed Image Text:Write a function valid Solution/ValidateSolution/valid_solution () that accepts a 2D array representing a Sudoku board, and returns true if it is a valid solution, or false otherwise. The cells of the sudoku board may also contain 0's, which will represent empty cells. Boards containing one or more zeroes are considered to be invalid solutions. The board is always 9 cells by 9 cells, and every cell only contains integers from 0 to 9. (More info at: http://en.wikipedia.org/wiki/Sudoku) #Using dict/hash-table from collections import defaultdict def valid_solution_hashtable(board): for i in range(len (board)): dict_row=defaultdict(int) dict_col=defaultdict(int) forj in range(len (board[0])): value_row = board [i][j] value_col=board [j][i] if not value_row or value_col== 0: return False if value_row in dict_row: return false else: dict_row[value_row] += 1 if value_col in dict_col: return False dict_col[value_col] += 1 else: for i in range(3): forj in range(3): grid_add = 0 fork in range(3): for l in range(3): grid_add += board [i*3 + k][j* 3+1] if grid_add != 45: return False return True # Without hash-table/dict def valid_solution (board): correct = [1, 2, 3, 4, 5, 6, 7, 8, 9] #check rows for row in board: if sorted (row) != correct: return false # check columns for column in zip (*board): if sorted (column) != correct: return false #check regions for i in range(3): for j in range(3): region = [] for line in board [i*3:(i+1)*3]: region += line[j*3:(j+1)*3] if sorted (region) != correct: return False # if everything correct return True # Using set def valid_solution_set(board): valid = set(range(1,10)) for row in board: if set(row)!= valid: return false for col in [[row[i] for row in board] for i in range(9)]: if set(col) != valid: return false for x in range(3): fory in range(3):
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY