Write a function in python called is_square(board): Given a board as a list of l
ID: 3681593 • Letter: W
Question
Write a function in python called is_square(board): Given a board as a list of lists of Booleans, check whether it represents a square board with the same number of rows and columns. If the rows don't all have the same number of items in them, or if the number of rows does not match the number of columns, this function returns False. Empty board is square.
o Assume: board is a list of lists of Booleans.
Examples:
is_square([[True,False,False],[False,False,False],[True,False,False]]) True
is_square([[True,False,False],[False,False,False]]) False #2 rows x 3 columns
is_square([[True],[False,False],[True,False,False]]) False #rows not same length
Explanation / Answer
def is_square(board):
if board is None:
return True
else:
numrows = len(board)
for element in board:
numcols = len(board[0])
if(numcols!=numrows):
return False
return True
print is_square([[True,False,False],[False,False,False],[True,False,False]])
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.