-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-matrix_divided.py
executable file
·35 lines (28 loc) · 1.29 KB
/
2-matrix_divided.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python3
"""Defining a matrix division function."""
def matrix_divided(matrix, div):
"""Dividing all elements of a matrix.
Args:
matrix (list): A list for lists of ints or floats.
div (int/float): The divisor.
Raises:
TypeError: If the matrix contains non-numbers.
TypeError: If the matrix contains rows of different sizes.
TypeError: If div is not an int or float.
ZeroDivisionError: If divisor is zero (0).
Returns:
Matrix representing devision results.
"""
if (not isinstance(matrix, list) or matrix == [] or
not all(isinstance(row, list) for row in matrix) or
not all((isinstance(ele, int) or isinstance(ele, float))
for ele in [num for row in matrix for num in row])):
raise TypeError("matrix must be a matrix (list of lists) of "
"integers/floats")
if not all(len(row) == len(matrix[0]) for row in matrix):
raise TypeError("Each row of the matrix must have the same size")
if not isinstance(div, int) and not isinstance(div, float):
raise TypeError("div must be a number")
if div == 0:
raise ZeroDivisionError("division by zero")
return ([list(map(lambda x: round(x / div, 2), row)) for row in matrix])