def is_valid_coord(board, r, c): Given a board and two ints, do r and c describe
ID: 3602060 • Letter: D
Question
def is_valid_coord(board, r, c): Given a board and two ints, do r and c describe a valid location on the board? Negative indexes are not allowed here (as a design decision).
• Assume:boardisavalidboard,randcareintegers.
• Hint: you should be using this function all over the place in the rest of your project!
• is_valid_coord([["B",".","R"],["R","R","B"]], 0, 0) • is_valid_coord([["B",".","R"],["R","R","B"]], 2, 3) • is_valid_coord([["B",".","R"],["R","R","B"]], -1,-1)
False
pop_out(board, c, color): Some variants of the game allow a player to remove the bottom piece in a column as their turn when that bottom piece is their color, called "popping out" the piece. Attempt to remove the bottom piece from the specified column, which will make any higher pieces drop down a spot. If the column is invalid, or there's nothing in the specified column, or it's not their color, make no changes and return False. Otherwise, perform the operation and return True.
• Assume: board is a valid board; c is an int, color is a color. • example:
>>> b = [['A','B'],['B','A']] >>> pop_out(b,0,'B')
True
>>> b
[['0','B'],['A','A']]
Explanation / Answer
def is_valid_coord(board,r,c):
if r < 0 or c < 0:
return False
a = len(board)
if a > 0:
b = len(board[0])
if r < a or c < b:
return True
else:
return False
else:
return False
def pop_out(board,c,color):
if (c < 0):
return False;
a = len(board)
if a > 1:
b = len(board[0])
if (c < b):
if color == board[len(board)-1][c]:
board[len(board)-1][c] = board[len(board)-2][c]
board[len(board)-2][c] = '0'
else:
return False
else:
return False
else:
return False
print(is_valid_coord([["B",".","R"],["R","R","B"]], 0, 0))
print(is_valid_coord([["B",".","R"],["R","R","B"]], 2, 3))
print(is_valid_coord([["B",".","R"],["R","R","B"]], -1,-1))
b = [['A','B'],['B','A']]
print(b)
pop_out(b,0,'B')
print(b)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.