-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-matrix_divided.py
executable file
·52 lines (40 loc) · 1.41 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python3
"""
2-matrix_divided module
Implement a funciton which divides all the elements
of a matrix
"""
def matrix_divided(matrix, div):
"""matrix_divided
Arguments:
matrix -- matrix
div -- division number
Raises:
TypeError: matrix must be a matrix (list of lists) of integers/floats
TypeError: Each row of the matrix must have the same size
TypeError: div must be a number
ZeroDivisionError: division by zero
Returns:
A new matrix each of its numbers divided by the div number
"""
mat_size = None
if not isinstance(matrix, list):
raise TypeError('matrix must be a matrix (list of lists)\
of integers/floats')
for lst in matrix:
if not isinstance(lst, list):
raise TypeError('matrix must be a matrix (list of lists)\
of integers/floats')
if mat_size is None:
mat_size = len(lst)
elif mat_size != len(lst):
raise TypeError('Each row of the matrix must have the same size')
for _ in lst:
if not isinstance(_, (float, int)):
raise TypeError('matrix must be a matrix (list of lists)\
of integers/floats')
if not isinstance(div, (float, int)):
raise TypeError('div must be a number')
if div == 0:
raise ZeroDivisionError('division by zero')
return [[round(x / div, 2) for x in lst] for lst in matrix]