-
Notifications
You must be signed in to change notification settings - Fork 0
/
q1_7.py
49 lines (39 loc) · 1000 Bytes
/
q1_7.py
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
#!/usr/bin/env python
def zeroRowsCols(arr, m=None,n=None) :
"""Modifies 2d list of values by putting zeros in all rows and cols that
have a 0 in them to begin with"""
rows = set() #initialize sets to track rows/cols to zero out
cols = set()
if m == None : m = len(arr)
if n == None : n = len(arr[0])
for i in range(m) :
for j in range(n) :
if arr[i][j] == 0 : #mark row + col of cell with 0
rows.add(i)
cols.add(j)
for i in range(m) : # run through array again and zero out marked cols/rows
for j in range(n) :
if (i in rows) or (j in cols) : #NOTE: if '!=' instead of 'or' then does xor
arr[i][j] = 0
def test_zero() :
a = [[1,1],
[1,1],
[1,1]]
b = [[1,0],
[1,0],
[1,1]]
zeroRowsCols(a)
zeroRowsCols(b)
assert a == [[1,1],
[1,1],
[1,1]]
assert b == [[0,0],
[0,0],
[1,0]]
print "passes tests"
if __name__ == '__main__' :
test_zero()
#incorporate
#############################
#Divide
#############################