Skip to content

Commit 448afcd

Browse files
committed
completed 1.8
1 parent 81fbcf4 commit 448afcd

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
11
""" 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. """
22

3+
def zero_matrix(matrix):
4+
""" Example input and output
35
6+
>>> zero_matrix([['x', 'x', 0, 'x'], ['x', 'x', 'x', 'x'], ['x', 'x', 'x', 'x']])
7+
[[0, 0, 0, 0], ['x', 'x', 0, 'x'], ['x', 'x', 0, 'x']]
8+
9+
>>> zero_matrix([])
10+
[]
11+
"""
12+
if len(matrix) > 0:
13+
for lst_i in xrange(len(matrix)):
14+
if len(matrix[lst_i]) > 0:
15+
for i in xrange(len(matrix[lst_i])):
16+
if matrix[lst_i][i] == 0:
17+
zero_index = i
18+
lst_index = lst_i
19+
break
20+
break
21+
22+
for lst in matrix:
23+
lst[zero_index] = 0
24+
for item in xrange(len(matrix[lst_index])):
25+
matrix[lst_index][item] = 0
26+
return matrix
427

528

629

730

831
if __name__ == "__main__":
9-
print "nothing yet"
32+
import doctest
33+
if doctest.testmod().failed == 0:
34+
print "tests pass"

0 commit comments

Comments
 (0)