Practice 01: 1D Array and Matrix Problems

Overview

In this practice, 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(items)
  • task02(items)
  • task03(listA, listB)
  • task04(items)
  • 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(items):
    """Counts the number of elements strictly smaller than the first element
 
    :param items: list of integers
    :return: integer, number of elements strictly smaller than the first element
 
    Examples:
 
    >>> task01([1, 2, 3])
    0
    >>> task01([3, 2, 1])
    2
    """   
    pass
 
 
def task02(items):
    """Checks if number of leading values 0 in list is equal to the number of trailing values 0.
 
    :param items: list of integers
    :return: boolean value
 
    Examples:
 
    >>> task02([0, 0, 0, 2, 3, 0, 0, 4, 5, 6, 0, 0])
    False
 
    >>> task02([0, 10, 11, 12, 22, 0])
    True
 
    >>> task02([0, 0, 0, 0, 0])
    True
    """
    pass
 
 
def task03(listA, listB):
    """Creates copy of listB and modifies it using following rule:
    All occurrences of maximum in listB are replaced by minimum of value of listA.
    Original listB remains unmodified.
 
    :param listA: list of integers
    :param listB: list of integers
    :return: list of integers, modified copy of listB
 
    Example:
 
    >>> listA = [2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5]
    >>> listB = [55, 20, 33, 11, 55, 33, 11, 33, 44, 44, 55]
    >>> task03(listA, listB)
    [2, 20, 33, 11, 2, 33, 11, 33, 44, 44, 2]
    """
    pass
 
 
def task04(items):
    """finds minimum of all values which appear in list exactly twice
    If no value appears twice in the list return None.
 
    :param items: list of integers
    :return: integer or None, minimum of all values which appear in list exactly twice
 
    Examples:
 
    >>> task04([11, 20, 33, 11, 20, 33, 11, 33, 44, 44, 55])
    20
    >>> task04([2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5]) is None
    True
    """
    pass
 
 
def task05(matrix):
    """ Shrinks the size of the matrix by 2:
    Removes the first and the last row of the matrix
    and the first and the last column  of the matrix.
    Returns modified copy of original matrix.
    Original matrix remains unchanged.
 
    :param matrix: matrix containing integer values
    :return: copy of original matrix shranked by 2
 
    Example:
 
    >>> inp_matrix = [
    ...     [1, 1, 1, 1, 1, 1],
    ...     [1, 2, 3, 2, 3, 1],
    ...     [1, 3, 2, 3, 2, 1],
    ...     [1, 2, 3, 2, 3, 1],
    ...     [1, 3, 2, 3, 2, 1],
    ...     [1, 1, 1, 1, 1, 1]
    ... ]
    >>> out_matrix = [
    ...     [2, 3, 2, 3],
    ...     [3, 2, 3, 2],
    ...     [2, 3, 2, 3],
    ...     [3, 2, 3, 2]
    ... ]
    >>> task05(inp_matrix) == out_matrix
    True
    """
    pass
 
 
def task06(matrix):
    """ Exchange the contents of the diagonals of the matrix.
    The items on the diagonals will stay in the same row,
    but in each row, the item X in the main diagonal will be exchanged with the
    item Y in the secondary diagonal (antidiagonal) in the same row.
 
    :param matrix: matrix containing integer values
    :return: modified copy of original matrix
 
    Example:
 
    >>> inp_matrix = [
    ...     [1, 0, 0, 0, 2],
    ...     [0, 1, 0, 2, 0],
    ...     [0, 0, 3, 0, 0],
    ...     [0, 2, 0, 1, 0],
    ...     [2, 0, 0, 0, 1]
    ... ]
    >>> out_matrix = [
    ...     [2, 0, 0, 0, 1],
    ...     [0, 2, 0, 1, 0],
    ...     [0, 0, 3, 0, 0],
    ...     [0, 1, 0, 2, 0],
    ...     [1, 0, 0, 0, 2]
    ... ]
    >>> task06(inp_matrix) == out_matrix
    True
    """
    pass
 
 
if __name__ == "__main__":
    import doctest
 
    doctest.testmod(verbose=True)
 

courses/be5b33pge/practices/01.txt · Last modified: 2025/02/25 15:49 by sindlpa1