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

Please use PYTHON only and follow all the following instructions carefully. Than

ID: 3596382 • Letter: P

Question

Please use PYTHON only and follow all the following instructions carefully. Thank you

Requirements

Background info (for your reference):

Problem statements:

Please make sure that it can pass all the test cases as follows: https://repl.it/Mvr6

Restrictions no modules may be imported. Allowed basic statements, variables, operators, del, indexing, slicing, in, are all allowed any form of control flow we've covered is allowed (if/else, loops, etc) only these built-in functions: range(), len(), int), str), list), abs () only these built-in methods: s.split), s.join(), s.pop(), xs.append), xs.extend), xs.insert(), s.format() calling other functions of the project (and your own helper functions). Please do this! Hint In our solution, we only used range, len, in, abs, .append(), .join(), .split(), and list() Remember: your grade is significantly based on passing test cases- try to completely finish individual functions before moving on. The easiest way to implement many functions is to call the earlier/easier functions, so it'll pay off to complete functions before moving on. Don't let yourself be "almost done" with a function, but miss all the tests!

Explanation / Answer

def init_board(num_rows, num_cols):
# create a new board
board = []
  
# add given number of rows
for i in range(num_rows):
# initialise each row with empty
row = ['.']*num_cols
board.append(row)
return board
# print(init_board(2, 3))

def show_board(board):
# create an empty string which will hold string representation of board
boardstr = ''
# for each row in board, craete its string representation
for row in board:
# string representation is just putting each cell one after another in row and then separating rows with new line
boardstr += ''.join(row) + ' '
# return string repreentation of board
return boardstr
# print(show_board([['.', '.', '.'], ['A', 'B', 'A']]))

def read_board(s):
# check if string is empty or None return None
if not s:
return None
  
# create an empty list which will hold all valid lines (non empty lines) in s
lines = []
  
# get all lines as a list by splitting lines on new line charatcer
tempLines = s.split(' ')
  
# for each line b=obtained by splitting keep only non empty lines
for line in tempLines:
# check if line is not None/empty
if line:
# append line to lines if it is non empty
lines.append(line)
  
# number of column will be entry in one line and all lines should have same length
col = len(lines[0])
row = 0
# for each lines
for i in range(len(lines)):
# check if length of line is different from other line, if yes return None
if len(lines[i]) != col:
return None
# if line is just white space don't consider it
if lines[i] == ' ':
continue
row += 1
# for each char in line
for c in lines[i]:
# check if char is in upper case letter, else its an invalid data
if not (c == '.' or c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
return None
  
# create an empty board as we now know exact number of rows and column
board = init_board(row, col)
  
# fill row and column from lines
for i in range(row):
for j in range(col):
board[i][j] = lines[i][j]
return board
# print(read_board(".... NOO "))

def get_size(board):
# check if board is not defined, if yes return None
if not board:
return None
# number of rows is length of outer list
row = len(board)
if row == 0:
return None
# length of column should be same for all column so taking out from first row
col = len(board[0])
return (row, col)
# print(get_size([['.', '.', '.'], ['A', 'B', 'A']]))

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
# 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))

def get_coords_by_color(board, color):
# list to hold all coordinates of a given colr in board
color_coord = []
# iterate over full board
for i in range(len(board)):
for j in range(len(board[i])):
# check if color is same as given color if yes then add coordinate as tuple in list
if board[i][j] == color:
color_coord.append((i, j))
return color_coord
# print(get_coords_by_color([['G', '.'], ['Y', 'Y']], 'Y'))
# print(get_coords_by_color([['G', '.'], ['Y', 'Y']], 'G'))
# print(get_coords_by_color([['.', 'X'], ['Y', 'X']], 'X'))

def get_colors(board):
colors = []
# iterate over full board and get all color present in board
for i in range(len(board)):
for j in range(len(board[i])):
# ignore empty cell or if color is already notes, else add it to list
if board[i][j] != '.' and (not board[i][j] in colors):
colors.append(board[i][j])
return colors
# print(get_colors([['.', 'Y'], ['Y', 'X']]))

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
# print(count_pieces_by_color([['.', 'Y'], ['Y', 'X']], 'Y'))

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

def pop_out(board, c, color):
(row, col) = get_size(board)
if c < 0 or c >= col or board[row-1][c] != color:
return False
# move all element down by removing last eleemnt of given column
for i in range(row-1, 0, -1):
board[i][c] = board[i-1][c]
board[0][c] = '.';
return True
# b = [['A', 'B'], ['B', 'A']]
# print(pop_out(b, 0, 'B'))
# print(b)

# copy pastable code link: https://paste.ee/p/Y58Z1