Code a function in python row_conflict(board,r): Given a board and an integer r
ID: 3681702 • Letter: C
Question
Code a function in python row_conflict(board,r): Given a board and an integer r indicating a row index, check and return True if a queen is already placed in row r; return False otherwise.
Assume: board is a list of lists of Booleans; board is square; r is an integer.
Hint: Remember, the board is zero-indexed, and negative indexes are not to be used.
Examples:
row_conflict([[True,False],[False,False]],0) True
row_conflict([[True,False],[False,False]],1) False
row_conflict([[True,False],[False,False]],-2) False # negatives not allowed
row_conflict([[True,False],[False,False]],5) False # index out of bound
Explanation / Answer
def row_conflict(board,r): row = len(board) column = len(board[0]) #print row ,":",column if r >= row or r >= column or r < 0: return False for i in xrange(0,column): if board[r][i]: return True return False # This is for testing the code if __name__ == "__main__": if row_conflict([[False,False,False],[False,False,True],[False,False,False]],1): print "True" else: print "False" if row_conflict([[True,False],[False,False]],1): print "True" else: print "False" if row_conflict([[True,False],[False,False]],-1): print "True" else: print "False" if row_conflict([[True,False],[False,True]],2): print "True" else: print "False" if row_conflict([[False,False,False],[False,False,True],[False,False,False]],0): print "True" else: print "False"Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.