-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-matrix_divided.py
More file actions
58 lines (38 loc) · 1.51 KB
/
Copy path2-matrix_divided.py
File metadata and controls
58 lines (38 loc) · 1.51 KB
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
53
54
55
56
57
58
#!/usr/bin/python3
"""
This module is composed by a function that divides the numbers of a matrix
"""
def matrix_divided(matrix, div):
""" Function that divides the integer/float numbers of a matrix
Args:
matrix: list of a lists of integers/floats
div: number which divides the matrix
Returns:
A new matrix with the result of the division
Raises:
TypeError: If the elements of the matrix aren't lists
If the elemetns of the lists aren't integers/floats
If div is not an integer/float number
If the lists of the matrix don't have the same size
ZeroDivisionError: If div is zero
"""
if not type(div) in (int, float):
raise TypeError("div must be a number")
if div == 0:
raise ZeroDivisionError("division by zero")
msg_type = "matrix must be a matrix (list of lists) of integers/floats"
if not matrix or not isinstance(matrix, list):
raise TypeError(msg_type)
len_e = 0
msg_size = "Each row of the matrix must have the same size"
for elems in matrix:
if not elems or not isinstance(elems, list):
raise TypeError(msg_type)
if len_e != 0 and len(elems) != len_e:
raise TypeError(msg_size)
for num in elems:
if not type(num) in (int, float):
raise TypeError(msg_type)
len_e = len(elems)
m = list(map(lambda x: list(map(lambda y: round(y / div, 2), x)), matrix))
return (m)