-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2D_elements_to_zero.py
More file actions
50 lines (34 loc) · 1.5 KB
/
2D_elements_to_zero.py
File metadata and controls
50 lines (34 loc) · 1.5 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
class Solution:
def setZeroes(self, matrix):
"""
Do not return anything, modify matrix in-place instead.
"""
if len(matrix) == 0 or matrix is None:
print (f"bad input..exiting")
return -1 #error code
m = len(matrix)
n = len(matrix[0])
if not (1 <= m <= 200):
raise ValueError("num of rows not 1 <= m <= 200")
if not (1 <= n <= 200):
raise ValueError("num of rows not 1 <= n <= 200")
for row in range(len(matrix)):
for col in range(0, row):
if not (pow(-2, 31) <= matrix[row][col] <= pow(2, 31)-1):
raise ValueError("matrix element out of bounds")
found = False
for row_index, row in enumerate(matrix):
if 0 in row:
found = True
#row_index = row_index
break
col_index = matrix[row_index].index(0)
print (f"row = {row_index}; col = {col_index}")
print (f"Matrix input: {matrix}")
if found == True:
matrix[row_index][:] = [0] * len(row)
for i, row in enumerate(matrix[:][col_index]):
matrix[i][col_index] = 0
print (f"Matrix after setting cols to 0: {matrix}")
matrix = [[1, 2, 3], [4, 0, 1], [7, 9, 9]]
Solution().setZeroes(matrix)