MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / add

Function add

matrix/matrix_operation.py:10–27  ·  view source on GitHub ↗

>>> add([[1,2],[3,4]],[[2,3],[4,5]]) [[3, 5], [7, 9]] >>> add([[1.2,2.4],[3,4]],[[2,3],[4,5]]) [[3.2, 5.4], [7, 9]] >>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]]) [[7, 14], [12, 16]] >>> add([3], [4, 5]) Traceback (most recent call last): ...

(*matrix_s: list[list[int]])

Source from the content-addressed store, hash-verified

8
9
10def add(*matrix_s: list[list[int]]) -> list[list[int]]:
11 """
12 >>> add([[1,2],[3,4]],[[2,3],[4,5]])
13 [[3, 5], [7, 9]]
14 >>> add([[1.2,2.4],[3,4]],[[2,3],[4,5]])
15 [[3.2, 5.4], [7, 9]]
16 >>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]])
17 [[7, 14], [12, 16]]
18 >>> add([3], [4, 5])
19 Traceback (most recent call last):
20 ...
21 TypeError: Expected a matrix, got int/list instead
22 """
23 if all(_check_not_integer(m) for m in matrix_s):
24 for i in matrix_s[1:]:
25 _verify_matrix_sizes(matrix_s[0], i)
26 return [[sum(t) for t in zip(*m)] for m in zip(*matrix_s)]
27 raise TypeError("Expected a matrix, got int/list instead")
28
29
30def subtract(matrix_a: list[list[int]], matrix_b: list[list[int]]) -> list[list[int]]:

Callers 1

mainFunction · 0.70

Calls 2

_check_not_integerFunction · 0.85
_verify_matrix_sizesFunction · 0.85

Tested by

no test coverage detected