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

PYTHON3 feel free to call previous functions, including one called \"def is_vali

ID: 3599598 • Letter: P

Question

PYTHON3

feel free to call previous functions, including one called "def is_valid_coord(board,r,c) which determines if a coordinate is actually on our board

def count_pieces_by_color(board,color): Given a board & color, count #pieces of that color. •Assume:boardisaboard;colorisacolor.

count_pieces_by_color([['.','Y'],['Y','X']], 'X')count_pieces_by_color([['.','Y'],['Y','X']], 'A')count_pieces_by_color(ex1, 'R')
count_pieces_by_color(ex1, 'Y')

1 0 8 8

def any_floating(board): Given a board, are any pieces floating? (Does any player-piece have a blank spot under it?)

Assume: board is a valid board other than the possibility of floating pieces. • any_floating([['X','Y'],['Y','X']]) False
any_floating([['X','Y'],['.','.'],['X','Y']]) True
any_floating(ex1) False

any_floating(ex4) True

def is_column_full(board, c): Is the specified column entirely filled with player pieces?
Assume: board is a valid board, c is an int.
is_column_full([['.','Y'] ,['Y','X'] ,['Y','X']],0) False
is_column_full([['.','Y'] ,['Y','X'] ,['Y','X']],99) False # not a col.is_column_full(ex1,3) True

is_column_full(ex1,4) False

def place_one(board, c, color): Attempt to play that color in that column, with the piece falling to the lowest open space. If the column is full or the column doesn't exist, don't modify board, and return False. If the column exists and has space for another piece, update the board to play the piece of that color in that column, and return True.

Assume: board is a valid board; c is an int; color is a color. • see session at end of document for another example usage.

Explanation / Answer

def is_valid_coord(board, r, c):
# a coordinate is valid if it is greater than equal to zero but strictl less than board size
if r < 0 or c < 0:
return False
(row, col) = get_size(board)
if r >= row or c >= col:
return False
  
return True

def count_pieces_by_color(board, color):
count = 0
# iterate over board
for i in range(len(board)):
for j in range(len(board[i])):
# if color in cell as asked color, increase its count
if board[i][j] == color:
count += 1
return count


def any_floating(board):
(row, col) = get_size(board)
# iterate over all column
for j in range(col):
# variable to make sure that once we found a color all below column should have color
color_found = False
# iterate over row
for i in range(row):
# if cell is not empty then it has color which means now all below cell should have color
if board[i][j] != '.':
color_found = True
elif board[i][j] == '.' and color_found: # if color is empty and above cell is filled meaning above ell was floating
return True
# return false if no cell is floating
return False

def is_column_full(board, c):
(row, col) = get_size(board)
if c < 0 or c >= col:
return False
# iterate over all row for given column
for i in range(row):
# if there is free space meaning coloumn is not full
if board[i][c] == '.':
return False
return True

# print(is_column_full([['.', 'Y'], ['Y', 'X'], ['Y', 'X']], 0))
# print(is_column_full([['.', 'Y'], ['Y', 'X'], ['Y', 'X']], 99))
# print(is_column_full([['.', 'Y'], ['Y', 'X'], ['Y', 'X']], 1))

def place_one(board, c, color):
(row, col) = get_size(board)
if c < 0 or c >= col or is_column_full(board, c):
return False
  
# iterate over all row from last for given column and see if it is empty, if it is but color at that cell
for i in range(row-1, -1, -1):
# if cell si empty put color
if board[i][c] == '.':
board[i][c] = color
return True
# return False if no cell was free
return False

# copy pastable link: https://paste.ee/p/OPsn5