Functions on python help Functions on python unset_location(puzzle, loc): given
ID: 3808613 • Letter: F
Question
Functions on python help
Functions on python unset_location(puzzle, loc): given a puzzle and a location (row, col), try to remove the value at that location (change to None). Return True if the puzzle was successfully changed (the value at that location is removed). Return False if the location is not valid, or there is currently no value at that location. unset_location ([[11], (0, 0)) rightarrow True # puzzle is now [[None]] unset_location ([[1]], (0, 1)) rightarrow False # location is invalid unset_location ([[None]], (0, 0)) rightarrow False # location is already None unset_location ([[1, 2], [1, 11], (1, 0)) rightarrow True # puzzle is now [[1, 2], [None, 1]] get size(puzzle): given a puzzle, return its size as an integer. You do not need to check whether the given puzzle is valid get_size([[None, None], [None, None]]) rightarrow 2 #2x2 puzzle get_size ([[1, 2, 3], [2, 1, 3], [None, None, 3]]) rightarrow 3 #3x3 puzzle get_size([[5]]) rightarrow 1 #1x1 puzzle get valid numbers (size): given a positive size, return a list of the numbers which can be validly placed in a puzzle of that size get_valid_numbers(2) rightarrow [1, 2] #2x2 puzzle get_valid_numbers(3) rightarrow [1, 2, 3] #3x3 puzzle get_valid_numbers (1) rightarrow [1] #1x1 puzzle contains only valid symbols(puzzle, complete): check if a puzzle contains only valid numbers. If the puzzle is incomplete, also allow None in the puzzle. complete will be a boolean indicating whether or not the puzzle is complete. You do not need to check that the puzzle is valid, only if the numbers used are correct. Contains_only_valid_symbols ([[1]], True) rightarrow True # contains 1 the only valid value contains_only_valid_symbols ([[None], True) rightarrow False # complete puzzle should not contain None contains only valid symbols ([[None]], False) rightarrow True # puzzle is incomplete so None is ok contains_only_valid_symbols ([[1, 1], [1, 1]], True rightarrow True # puzzle contains only valid values (1s and 2s) has repeat(xs, v): given a list of numbers xs and a number n, check whether xs has repeated values of n. Return True if v occurs more than once in xs, return False otherwise. You may not use .count ()! Has_repeat ([None], 1) rightarrow False has_repeat ([1, 2, 3], 1) rightarrow False has_repeat ([1, 2, 1], 1) rightarrow True get_row (puzzle, row index): return the row at the specified row_index from puzzle as a list. Return None if the row index is invalid get_row ([[1, None], [2, None 0) rightarrow [1, None] # row 0 get _row([[1, None], [2, None]], 1) rightarrow [2, None] # row 1 get_row ([[1, None], [2, None]], 2) rightarrow None # invalid indexExplanation / Answer
I could not answer 1st and 4th part of the question. But i have answered 2nd, 3rd, 5th and 6th part of question.
Please find below the solution:
def get_size(puzzle):
# Return the size of the puzzle.
return len(puzzle)
def get_valid_numbers(size):
# Return the list of numbers which can be validly placed in the puzzle of that size.
numbers = []
i = 1
while(i<=size):
numbers.append(i)
i = i + 1
return numbers
def has_repeat(xs,v):
#Setting a counter equal to zero.
count = 0
#Looping over the list xs and comparing v with it.
for i in xs:
if v == i:
count = count + 1
if count>1:
return True
else:
return False
def get_row(puzzle, row_index):
if row_index >= len(puzzle):
return 'None'
else:
return puzzle[row_index]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.