-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1.8-zero-matrix.py
More file actions
34 lines (26 loc) · 1009 Bytes
/
1.8-zero-matrix.py
File metadata and controls
34 lines (26 loc) · 1009 Bytes
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
""" 1.8 Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0. """
def zero_matrix(matrix):
""" Example input and output
>>> zero_matrix([['x', 'x', 0, 'x'], ['x', 'x', 'x', 'x'], ['x', 'x', 'x', 'x']])
[[0, 0, 0, 0], ['x', 'x', 0, 'x'], ['x', 'x', 0, 'x']]
>>> zero_matrix([])
[]
"""
if len(matrix) > 0:
for lst_i in xrange(len(matrix)):
if len(matrix[lst_i]) > 0:
for i in xrange(len(matrix[lst_i])):
if matrix[lst_i][i] == 0:
zero_index = i
lst_index = lst_i
break
break
for lst in matrix:
lst[zero_index] = 0
for item in xrange(len(matrix[lst_index])):
matrix[lst_index][item] = 0
return matrix
if __name__ == "__main__":
import doctest
if doctest.testmod().failed == 0:
print "tests pass"