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

Help with these functions,python Make an \"empty\" n by n board. Empty boards ar

ID: 3808356 • Letter: H

Question

Help with these functions,python

Make an "empty" n by n board. Empty boards are filled with None. Assume n > 0. is_valid_location(loc, puzzle): given a location tuple (row, col) determine if those coordinates are valid locations in the puzzle. You do not need to check if value is valid. is_complete(puzzle): determine if the puzzle has any remaining None locations. You do not need to check if the puzzle is a valid solution. get_value_at_location(puzzle, loc): given a location as a tuple (row, col), return the value at that location of the given puzzle. You may assume that the location provided is a valid location. set_location(puzzle, value, loc): given a puzzle, a value, and a location (row, col), try to set the puzzle location to that value. If the puzzle location is empty, set the value and return True. If the location is already set, or the location is invalid, do not modify the puzzle and return False. You do not need to check if value is valid.

Explanation / Answer

make_empty_board : Create a 2-dimensional array with the values initialized to None. Here is the function implementation.
def make_empty_board(n):
   board = [ [None for row in range(n)] for col in range(n) ]
   return board

is_valid_location : Just check if the location values are within the dimensions of the puzzle board i.e, check if loc[0] is more than 0 and less than the rows of the puzzle. Similarly loc[1] is more than 0 and less than cols of the puzzle. Return True if both are valid, False otherwise
def is_valid_location(loc, puzzle):
   puzzle_rows = len(puzzle)
   puzzle_cols = len(puzzle[0])
   is_valid_row = loc[0] >= 0 && loc < puzzle_rows
   is_valid_col = loc[1] >= 0 && loc < puzzle_cols
   return is_valid_row && is_valid_col

is_complete : Use any function to check if the puzzle array has any None element.
def is_complete(puzzle):
   return any(None in row for row in puzzle)

get_value_at_location : Just return the value located at the given loc of the puzzle. You don't have to validate the location since the question says you can assume the values passed in is a valid location.
def get_value_at_location(puzzle, loc):
   return puzzle[loc[0]][loc[1]]

set_location : First check if the location is valid with a call to is_valid_location, then check if the location is already set. Return False if location is not valid or location is already set. If not set the value and return True.

def set_location(puzzle, value, loc):
   is_valid_loc = is_valid_location(loc, puzzle)
   is_set = puzzle[loc[0]][loc[1]] is not None

   return False if is_set || not is_valid_location

   puzzle[loc[0]][loc[1]] = value
   return 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