Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please help me code this for python!! I have attached my code below as you will

ID: 3808976 • Letter: P

Question

please help me code this for python!! I have attached my code below as you will need it to complete the rest of the functions!! Thank alot!!!!

def make_empty_board(n):
l=[]
for i in range(n):
l2=[]
for j in range(n):
l2.append(None)
l.append(l2)
return l
def is_valid_location(loc, puzzle):   
dim = len(puzzle)
x,y = loc
if (x<dim) & (-1<x) & (y<dim) & (-1<y) :
return True
else:
return False
def is_complete(puzzle):
for x in range(len(puzzle)):
for y in range(len(puzzle)):
if puzzle[x][y] == None:
return False
return True
def get_value_at_location(puzzle, loc):
x,y=loc
return puzzle[x][y]
def set_location(puzzle, value, loc):
x,y=loc
if is_valid_location(loc, puzzle):
puzzle[x][y]=value
return True
else:
return False
def unset_location(puzzle, loc):
l_index=loc[0]
ele_index=loc[1]
try:
if puzzle[l_index][ele_index]==None:
return False
puzzle[l_index][ele_index]=None
except Exception as e:
return False
else:
return True
def get_size(puzzle):
size=len(puzzle[0])
return size
def get_valid_numbers(size):
l=[]
for i in range(1,size+1):
l.append(i)
return l
def contains_only_valid_symbols(puzzle,complete):
if complete == True:
for lst in puzzle:
for i in lst:
if isinstance(i,int):
continue
else:
return False
else:
return True
else:
for lst in puzzle:
for i in lst:
if isinstance(i,int) or i==None:
continue
else:
return False
else:
return True
def has_repeat(xs,v):
c=xs.count(v)
if c > 1 :
return True
else:
return False
def get_row(puzzle,row_index):
try:
return puzzle[row_index]
except Exception as e:
return None

complete): given a puzzle, determine if the row at row index is a is valid row(puzzle row index valid set of numbers (containing only valid values and unique values). complete is a boolean indicating if the row may contain None values. As before, if the puzzle is incomplete, also allow None, if it is complete, do not allow None. Also note that it is OK to have multiple None values in one row. (HINT use get valid numbers get row() and has repeat [2,4,None ,None 1,[1, 2,3,3],[3,1 ,None,5],[4,3 ,None, None board is valid row (board, 0, False) True ok to have None if incomplete is valid row (board, 0 True) False cannot have None if complete is valid row (board, 1, True False two 3s in same ro is valid row(board, 2 False) False invalid value 5 is valid row(board, 2, True False not even a valid ro complete): determine if all the rows in puzzle are valid. complete is a boolean has valid rows (puzzle indicating if the rows should not contain None values. If incomplete allow None, if complete, do not allow None. (HINT: use get row() and is valid row() as part of your solution) has valid rows [1, 2],[1,21], True True has valid rows ([[1, None],[1,21], False) True has valid rows ([[1,None 1,[1, 211, True False cannot have None if complete has valid rows([[1,4],[ 1,211, True) False row 0 has a 4 in a 2x2 puzzle get column (puzzle col index) return the column with specified col index from puzzle as a list. Return None if col index is invalid get column ([[1,None],[2,Nonell, 0) [1 col 0 get column ([[1,None 1,[2,None 11, 1) [None, None] col 1 get column ([[1,None 1,[2,None] 1, 2) invalid index None complete) similar to is valid row but checking the validity of is valid col (puzzle col index specified column instead. (HINT: use get valid numbers get column and has repeat().) [2,1,None ,None ],[None, 2,3,3],[3, 1, None,5],[4,3,None,None board is valid col(board, 0, False) True ok to have None if incomplete is valid col (board, 0, True) False cannot have None if complete is valid col board, 1 True) False two 1s in same column is valid col(board, 3, False) False invalid value 5 is valid col(board, 7 True False not even a valid column complete): determine if all the columns in puzzle are valid. complete is a has valid cols (puzzle boolean indicating if the columns may contain None values. If incomplete allow None, if complete, do not allow None. (HINT use is valid col() and get column() as part of your solution) has valid cols ([[1,2],[1,1]], True) False two 1s in col 0 has valid cols([[1,None],[2,11], False) True has valid None ],[2,111, True) False cannot have None if complete has valid cols([[1,4],[2,11], False col 1 has a 4 in a 2x2 puzzle True

Explanation / Answer

# code link incase indentation mess up: https://goo.gl/WnUPTG

def get_valid_numbers(n):
return range(1, n+1)

def get_row(puzzle, row_index):
if row_index >= len(puzzle) or row_index < 0:
return None
return puzzle[row_index]

def has_repeat(xs, x):
count = 0
for n in xs:
if n == x:
if count == 1:
return True
else:
count = 1
return False

def is_valid_row(puzzle, row_index, complete):
row = get_row(puzzle, row_index)
if row == None:
return False
valid_numbers = get_valid_numbers(len(row))

for n in row:
if complete and (n == None):
return False
  
if n == None:
continue
  
if (not n in valid_numbers):
return False
  
if has_repeat(row, n):
return False
return True

def has_valid_rows(puzzle, complete):
for i in range(0, len(puzzle)):
if not is_valid_row(puzzle, i, complete):
return False
return True

def get_column(puzzle, col_index):
if col_index < 0 or col_index >= len(puzzle):
return None
  
col = []
  
for row in puzzle:
col.append(row[col_index])
return col

def is_valid_col(puzzle, col_index, complete):
col = get_column(puzzle, col_index)
if col == None:
return False
valid_numbers = get_valid_numbers(len(col))

for n in col:
if complete and (n == None):
return False
  
if n == None:
continue
  
if (not n in valid_numbers):
return False
  
if has_repeat(col, n):
return False
return True

def has_valid_cols(puzzle, complete):
for i in range(0, len(puzzle)):
if not is_valid_col(puzzle, i, complete):
return False
return True

def _apply_operation(val_list, op):
  
if op == '+':
total = 0
for v in val_list:
total += v
return total
if op == '-':
total = val_list[0]
for v in val_list[1:]:
total -= v
total = abs(total)
return total
if op == 'x':
total = 1
for v in val_list:
total *= v
return total
def is_valid_cage_solution(puzzle, op, expected_total, locations):
values = []
for (i, j) in locations:
values.append(puzzle[i][j])
return _apply_operation(values, op) == expected_total

def is_valid(puzzle, cages, complete):
is_valid_puzzle = has_valid_cols(puzzle, complete) and has_valid_rows(puzzle, complete)
is_valid_cage = True
if complete:
is_valid_cage= is_valid_cage_solution(puzzle, cages[0][0], cages[0][1], cages[0][2])
return is_valid_puzzle and is_valid_cage

def get_board_string(puzzle):
board_str = " "
for i in range(0, len(puzzle)):
board_str += "[" + str(i) + "] "
board_str += " "
board_str += " -"
for i in range(0, 4*len(puzzle)):
board_str += "-"
board_str += " "
for i in range(0, len(puzzle)):
board_str += "[" + str(i) + "] |"
for j in range(0, len(puzzle[i])):
board_str += " "
if puzzle[i][j] == None:
board_str += "."
else:
board_str += str(puzzle[i][j])
board_str += " |"
board_str += " "
board_str += " -"
for i in range(0, 4*len(puzzle)):
board_str += "-"
return board_str

print("is_valid_row")
board = [[2,4,None, None], [1,2,3,3], [3,1,None, 5], [4,3, None, None]]
print(is_valid_row(board, 0, False))
print(is_valid_row(board, 0, True))
print(is_valid_row(board, 1, True))
print(is_valid_row(board, 2, False))
print(is_valid_row(board, -2, True))

print()

print("has_valid_row")
print(has_valid_rows([[1,2],[1,2]], True))
print(has_valid_rows([[1,None],[1,2]], False))
print(has_valid_rows([[1,None],[1,2]], True))
print(has_valid_rows([[1,4],[1,2]], True))

print()
print ("get_column")
print(get_column([[1,None],[2,None]], 0))
print(get_column([[1,None],[2,None]], 1))
print(get_column([[1,None],[2,None]], 2))

print ()
print ("is_valid_col")
board = [[2,1,None,None],[None,2,3,3],[3,1,None,5],[4,3,None,None]]
print(is_valid_col(board, 0, False))
print(is_valid_col(board, 0, True))
print(is_valid_col(board, 1, True) )
print(is_valid_col(board, 3, False))
print(is_valid_col(board, 7, True))

print()
print("has_valid_cols")
print(has_valid_cols([[1,2],[1,1]], True))
print(has_valid_cols([[1,None],[2,1]], False))
print(has_valid_cols([[1,None],[2,1]], True))
print(has_valid_cols([[1,4],[2,1]], True))

print()
print("is_valid_cage_solution")
print(is_valid_cage_solution([[1]], '+', 1, [(0,0)]))
print(is_valid_cage_solution([[1,2],[2,1]], '-', 1, [(0,0),(1,0)]))
print(is_valid_cage_solution([[1,2],[2,1]], '-', 1, [(1,0),(0,0)]))
print(is_valid_cage_solution([[1,2],[2,1]], 'x', 4, [(0,0),(0,1),(1,0),(1,1)]))
print(is_valid_cage_solution([[1,1],[1,1]], 'x', 1, [(0,0)]))
print(is_valid_cage_solution([[1,2],[2,1]], '+', 4, [(0,0),(0,1)]))

print()
print("Is_valid")
board_1 = [[3,1,2],[2,3,1],[1,2,3]]
board_2 = [[3,1,2],[2,2,1],[1,2,3]]
print(is_valid(board_1, [], False))
print(is_valid(board_1, [['+',5,[(0,0),(1,0)]]], True))
print(is_valid(board_1, [['+',4,[(0,0),(1,0)]]], True))
print(is_valid(board_2, [['+',5,[(0,0),(1,0)]]], True))
print(is_valid([[None, None],[None,None]], [['+',5,[(0,0),(1,0)]]], False))

print(get_board_string([[3,1,2],[2,None,None],[None,None,None]]))