Practices 02: 2D Array (Matrix) Processing

Overview

In practice 02, you will create a single Python file main.py. In this file you will write (or complete) the following functions, each of which solves a particular problem:

  • task01(matrix)
  • task02(matrix)
  • task03(matrix)
  • task04(matrix)
  • task05(matrix)
  • task06(matrix)

Both filename and names of the functions have to be exactly the same as described.

When submitting your solution:

  1. Zip the file main.py into .zip archive with arbitrary name.
  2. Upload the .zip file to the BRUTE evaluation system.

Grading

You can obtain maximum of 2 points.

Testing Your Code Localy

In starter code which is provided bellow each function contains docstring with short desription. Docstrings also contain examples of usage. Those examples can be used for testing your functions using doctest module. To run those tests you can simply run the main module as main script.

Starter Code

Below is a basic code structure you can use. Copy-paste the following code and complete each function (task01, task02, task03, …) according to description in the function docstring. Then submit your final main.py.

import copy
 
def task01(matrix):
	"""For the given matrix, returns list containing
	the indexes of columns with exactly two distinct values in it.
	If there is no such column, returns None.
 
	:param matrix: matrix containing integer values
	:return: None or list containing the indexes of columns
 
	Examples:
 
	>>> inp_matrix = [
	...     [30, 14, 25, 19, 22, 18, 16, 24, 28, 13, 22],
	...     [19, 14, 32, 33, 35, 18, 21, 32, 19, 13, 12],
	...     [19, 14, 17, 40, 35, 15, 16, 24, 28, 18, 22],
	...     [31, 14, 17, 19, 22, 15, 21, 24, 28, 13, 22],
	...     [30, 22, 25, 17, 35, 18, 21, 24, 37, 32, 22],
	...     [30, 14, 17, 34, 35, 15, 16, 24, 28, 18, 34]
	... ]
	>>> task01(inp_matrix)
	[1, 4, 5, 6, 7]
 
	>>> inp_matrix = [
	...     [10, 10, 10],
	...     [20, 20, 20],
	...     [30, 30, 30]
	... ]
	>>> task01(inp_matrix) is None
	True
	"""
	pass
 
 
def task02(matrix):
	"""finds maximum of sums of all submatrices of size  2x2
	and counts the number of occurrences of submatrices with this maximum in the whole matrix.
	The sum of a submatrix is the sum of all its elements.
	Function returns tuple containing:
	The maximum sum of a 2x2 submatrix of the input matrix
	and the number of occurrences of a submatrices with this sum in the input matrix.
 
	:param matrix: matrix containing integer values
	:return: tuple containing:
			- maximum sum of a 2x2 submatrix of the input matrix (int)
			- number of occurrences of a submatrices with this sum in the input matrix (int)
 
	Example:
 
	>>> inp_matrix = [
	...     [0, 1, 0, 2, 1],
	...     [1, 0, 2, 0, 1],
	...     [2, 1, 0, 0, 1]
	... ]
	>>> task02(inp_matrix)
	(4, 3)
	"""
	pass
 
 
def task03(matrix):
	"""
	Creates a copy of matrix and then performs following modifications:
	Substitute each zero value found on position [i][j] by sum of all values
	which are above this zero in the same column j in original matrix
	and to the right of this zero in the same row i in original matrix.
 
	:param matrix: square matrix containing integers
	:return: modified copy of the original matrix
 
	Examples:
	>>> inp_matrix = [
	...     [1, 1, 1, 0, 2, 3],
	...     [2, 0, 1, 0, 0, 2],
	...     [1, 0, 0, 0, 0, 0],
	...     [0, 2, 2, 2, 0, 0]
	... ]
	>>> out_matrix = [
	...     [1, 1, 1, 5, 2, 3],
	...     [2, 4, 1, 2, 4, 2],
	...     [1, 1, 2, 0, 2, 5],
	...     [10, 2, 2, 2, 2, 5]
	... ]
	>>> task03(inp_matrix) == out_matrix
	True
	"""
	pass
 
 
def task04(matrix):
	"""
	Creates copy of input square matrix and modifies it in the following way:
	The contents of the diagonals are rotated 90 degrees to the left
	(anti-clockwise) around the center of the matrix.
	Values which are not part of main or secondary diagonal remain unmodified.
	The examples below illustrate the meaning of
	"rotation of diagonals 90 degrees to the left."
 
	:param matrix: square matrix containing integers
	:return: modified copy of the original matrix
 
	Examples:
 
	>>> inp_matrix = [
	...     [1, 0, 0, 0, 0, 0, 4],
	...     [0, 1, 0, 0, 0, 4, 0],
	...     [0, 0, 1, 0, 4, 0, 0],
	...     [0, 0, 0, 5, 0, 0, 0],
	...     [0, 0, 2, 0, 3, 0, 0],
	...     [0, 2, 0, 0, 0, 3, 0],
	...     [2, 0, 0, 0, 0, 0, 3]
	... ]
	>>> out_matrix = [
	...     [4, 0, 0, 0, 0, 0, 3],
	...     [0, 4, 0, 0, 0, 3, 0],
	...     [0, 0, 4, 0, 3, 0, 0],
	...     [0, 0, 0, 5, 0, 0, 0],
	...     [0, 0, 1, 0, 2, 0, 0],
	...     [0, 1, 0, 0, 0, 2, 0],
	...     [1, 0, 0, 0, 0, 0, 2]
	... ]
	>>> task04(inp_matrix) == out_matrix
	True
 
	>>> inp_matrix = [
	...     [10, 33, 44, 33, 44, 21],
	...     [33, 11, 33, 44, 20, 44],
	...     [44, 33, 12, 19, 44, 33],
	...     [33, 44, 18, 13, 33, 44],
	...     [44, 17, 44, 33, 14, 33],
	...     [16, 44, 33, 44, 33, 15]
	... ]
	>>> out_matrix = [
	...     [21, 33, 44, 33, 44, 15],
	...     [33, 20, 33, 44, 14, 44],
	...     [44, 33, 19, 13, 44, 33],
	...     [33, 44, 12, 18, 33, 44],
	...     [44, 11, 44, 33, 17, 33],
	...     [10, 44, 33, 44, 33, 16]
	... ]
	>>> task04(inp_matrix) == out_matrix
	True
	"""
	pass
 
 
def task05(matrix):
	"""
	Creates copy of the input square matrix of integers
	and modifies it in the following way:
	All values which are at the border of the matrix
	are equal to the value at position [0][0] in the original matrix.
	All values which are at distance 1 from the border of the matrix
	are equal to the value at position [1][1] in the original matrix.
	All values which are at distance 2 from the border of the matrix
	are equal to the value at position [2][2] in the original matrix.
	And so on, up to the center of the matrix.
	In general:
	All values which are at distance k ( 0 <= k <= (N-1)//2 )
	from the border of the matrix are equal to the value
	at position [k][k] in the original matrix.
	The distance of a position [x][y] to the border is the
	minimum number of other positions between the border
	and the given position, in any of the four directions - up, down, left, right.
 
	:param matrix: square matrix containing integers
	:return: modified copy of the original matrix
 
	Examples:
 
	>>> inp_matrix = [
	...     [2, 3, 4, 5, 6, 7, 1],
	...     [1, 6, 3, 4, 5, 6, 7],
	...     [7, 6, 5, 4, 3, 2, 1],
	...     [4, 4, 4, 4, 4, 4, 4],
	...     [2, 1, 2, 1, 2, 1, 3],
	...     [1, 2, 3, 4, 5, 6, 7],
	...     [4, 4, 5, 5, 6, 1, 2]
	... ]
	>>> out_matrix = [
	...     [2, 2, 2, 2, 2, 2, 2],
	...     [2, 6, 6, 6, 6, 6, 2],
	...     [2, 6, 5, 5, 5, 6, 2],
	...     [2, 6, 5, 4, 5, 6, 2],
	...     [2, 6, 5, 5, 5, 6, 2],
	...     [2, 6, 6, 6, 6, 6, 2],
	...     [2, 2, 2, 2, 2, 2, 2]
	... ]
	>>> task05(inp_matrix) == out_matrix
	True
	"""
	pass
 
 
def task06(matrix):
	"""
	You are given an N×M matrix with N>3 and M>3 filled with cells of value:
	- 0: Represents air (water can pass through).
	- 1: Represents a roof (water cannot pass through).
	- 2: Represents water
	The first row of the matrix is always filled with water (2).
	The last  row of the matrix is always filled with air (0).
	Water can flow from the top row towards the bottom row by moving in any of the following directions:
	1) Down (directly below the current cell)
	2) Left (horizontally left if landed on roof)
	3) Right (horizontally right if landed on roof)
	Determine whether water can reach the last row of the matrix.
	Return False indicating the roof leaks, otherwise, return True indicating the roof is leak-proof.
	:param matrix: N×M matrix containing values 0, 1 or 2.
	:return: False if water reaches last row, True otherwise.
 
	Example:
	>>> inp_matrix = [
	...     [2, 2, 2],
	...     [1, 1, 1],
	...     [0, 0, 0]]  # <- Can water (2) reach this row?
	>>> task06(inp_matrix)
	True
 
	>>> inp_matrix = [
	...     [2, 2, 2, 2],
	...     [1, 0, 0, 1],
	...     [0, 1, 0, 0],
	...     [0, 0, 1, 0],
	...     [0, 0, 0, 0]]  # <- Can water (2) reach this row?
	>>> task06(inp_matrix)
	False
 
	>>> inp_matrix = [
	...     [2, 2, 2],
	...     [0, 1, 1],
	...     [0, 0, 0],
	...     [1, 1, 0],
	...     [0, 0, 0]]  # <- Can water (2) reach this row?
	>>> task06(inp_matrix)
	False
	"""
	pass
 
 
if __name__ == "__main__":
	import doctest
	doctest.testmod(verbose=True)

courses/be5b33pge/practices/02.txt · Last modified: 2025/02/26 15:55 by sindlpa1