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

I need help on these functions. PYTHON3 Is_valid_row(puzzle, row_index, complete

ID: 3809400 • Letter: I

Question

I need help on these functions. PYTHON3

Is_valid_row(puzzle, row_index, complete): given a puzzle, determine if the row at row_index is a 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. board = [[2, 4, None, None], [1, 2, 3, 3], [3, 1, None, 5], [4, 3, None, None]] is_valid_row(board, theta, False) Rightarrow True # ok to have None if incomplete is_valid_row(board, theta, True) Rightarrow False # cannot have None if complete is_valid_row(board, 1, True) Rightarrow False # two 3s in same row is_valid_row(board, 2, False) Rightarrow False # invalid value 5 is_valid_row(board, -2, True) Rightarrow False # not even a valid row has_valid_rows (puzzle, complete): determine if all the rows in puzzle are valid. complete is a boolean indicating if the rows should not contain None values. If incomplete allow None complete, do not allow None. Has_valid_rows ([[1, 2], [1, 2]], True) Rightarrow True has_valid_rows ([[1, None], [1, 2]], False) Rightarrow True has_valid_rows ([[1, None], [1, 2]], True) Rightarrow False # cannot have None if complete has_valid_rows ([[1, 4], [1, 2]], True) Rightarrow False # row theta has a 4 in a 2 times 2 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, None]], theta) Rightarrow [1, 2] # col theta get column ([[1, none], [2, none]], 1) Rightarrow None # invalid index is_valid_col(puzzle, col_index, complete):similar to is_valid_row but checking the validity of specified column instead. board = [[2, 1, None, None], [None, 2, 3, 3], [3, 1, None, 5], [4, 3, None, None]] is_valid_col(board, theta, False) Rightarrow True # ok to have None if incomplete is_valid col(board, theta, True) Rightarrow False # cannot have None if complete is_valid_col (board, 1, True) Rightarrow False # two 1s in same column is_valid_col (board, 3, False) Rightarrow False # invalid value 5 is_valid_col (board, 7, True) Rightarrow False # not even a valid column has_valid_cols(puzzle, complete): determine if all the columns in puzzle are valid. complete is a boolean indicating if the columns may contain None values. If incomplete allow None, if complete, do not allow None. Has_valid_cols([[1, 2], [1, .1]], True) Rightarrow False # two 1s in col theta has_valid_cols ([[1, None], [2, 1]], False) Rightarrow True has_valid_cols([[1, None], [2, 1]], True) Rightarrow False # cannot have None if complete has_valid_cols ([[1, 4], [2, 1]], True) Rightarrow False #col 1 has a 4 in a 2 times 2 puzzle is_valid_cage_olution(puzzle, op, expected_total, locations): given a puzzle, an operation op, an integer expected_total, and a list of location tuples (e.g. a "cage" in a KenkKen puzzle), determine if the puzzle locations total correctly for the operation. You do not need to check if the rows/columns of the puzzle are valid.

Explanation / Answer

# online code link in case indentation mess up https://goo.gl/0YioUl
# specification for is_valid_cage_solutions is not clear

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

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))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote