Please help with solving / explaing these codes! Python 3: the codes needed to c
ID: 3809257 • Letter: P
Question
Please help with solving / explaing these codes! Python 3: the codes needed to complete this are:
def get_column(puzzle,col_index):
size = get_size(puzzle)
if col_index + 1 > size:
return None
lst = []
for i in range(size):
lst.append(puzzle[i][col_index])
return lst
def get_valid_numbers(size):
validList = []
for i in range(1, size + 1):
validList.append(i)
return validList
def is_valid_location(loc, puzzle):
num = len(puzzle)
(row, col) = loc
if (row < num) and (-1 < row) and (col < num) and (-1 < col):
return True
else:
return False
def get_missing_numbers_row_or_col()
get missing numbers row or col(xs): determine which values are missing from the specified row (or column). The numbers should be returned in order. Note that if a column is passed in, it will be as a list (as is the return from get column(). (HINT: use get valid numbers to as part of your solution.) o get missing numbers row or col (C1.2]) no values missing o get missing numbers row or col(C1,None, 21) 131 value mising o get missing numbers-row or col [1,2,31 all missing o get missing numbers row or col([None,None,1]) [2,3] 1 is in place o get missing numbers row or col (C1.1 11) [2,3] 1 is in place candidates at location (puzzle, loc): return a list of suggested values for the given location (row, column) based on missing value checking of row and col. If the location is invalid or already contains a value, return None. (HINT: use is valid location and get missing numbers row or col o candidates at location ([[None (0,0)) [1] o candidates at location (CE1]], (0,0)) None location already contains a value o candidates at location ([[None (1,1)) None location is invalid o candidates at location (CC1,2,31,[2,3,1],[3,None, Nonel 1, (2,1)) [1,21 valid for row, only [1] valid for column o candidates at location (CC1,2,31,[1,3,2],[1,None, Nonel 1, (2,1)) [2,31 valid for row [1] valid for column is symmetric (puzzle) check whether the given puzzle is symmetric with respect to the main diagonal; return True if it is symmetric and False otherwise. See examples below. Restriction: must not modify the original puzzle! board 1 [[3,1,2],[1,3,1],[2, 1,311 board 2 [[3,1,2],[2,3,1] 1,2,3]] o is symmetric (board 1) True o is symmetric (board 2) False 1 2 board 1: symmetric board 2: not symmetricExplanation / Answer
Part 1)
def get_valid_numbers(size):
validList = []
for i in range(1, size + 1):
validList.append(i)
return validList
def get_missing_numbers_row_or_col(xs):
return ( list(set(get_valid_numbers(len(xs))) - set(xs)) )
Sample Output:
Part 2
def get_valid_numbers(size):
validList = []
for i in range(1, size + 1):
validList.append(i)
return validList
def get_missing_numbers_row_or_col(xs):
return ( list(set(get_valid_numbers(len(xs))) - set(xs)) )
def is_valid_location(loc, puzzle):
num = len(puzzle)
(row, col) = loc
if (row < num) and (-1 < row) and (col < num) and (-1 < col):
return True
else:
return False
def candidate_at_location(l, a , b ):
loc= (a,b)
if(is_valid_location(loc,l)):
if(l[a][b] != None):
return None
else:
valid_for_row = get_missing_numbers_row_or_col(l[a])
col = []
for i in range(0,len(l)):
col.append(l[i][b])
valid_for_col = get_missing_numbers_row_or_col(col)
return list(set(valid_for_row).intersection(set(valid_for_col)))
else:
return None
print(candidate_at_location([[None]], 0 , 0 ))
print(candidate_at_location([[1]], 0 , 0 ))
print(candidate_at_location([[None]], 1 , 1 ))
print(candidate_at_location([[1,2,3],[2,3,1],[3,None,None]], 2 , 1 ))
print(candidate_at_location([[1,2,3],[1,3,2],[1,None,None]], 2 , 1 ))
Sample Output:
Part 3:
def is_symmetric(puzzle):
for i in range(0,len(puzzle)):
for j in range(0,len(puzzle)):
if(puzzle[i][j] == puzzle[j][i]):
pass
else:
return False
return True
puzzle1 = [[3,1,2], [1,3,1], [2,1,3] ];
puzzle2 = [[3,1,2], [2,3,1], [1,2,3] ];
print(is_symmetric(puzzle1))
print(is_symmetric(puzzle2))
Sample Output: