forked from thatguyintech/100-day-coding-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwritex.py
78 lines (61 loc) · 1.62 KB
/
writex.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def writeX(matrix):
rowsToChange = set()
colsToChange = set()
height = len(matrix)
width = len(matrix[0])
for i in xrange(height):
for j in xrange(width):
if matrix[i][j] == 'x':
rowsToChange.add(i)
colsToChange.add(j)
for row in rowsToChange:
for j in xrange(width):
if matrix[row][j] != 'x':
matrix[row][j] = 'x'
for i in xrange(height):
for col in colsToChange:
if matrix[i][col] != 'x':
matrix[i][col] = 'x'
return matrix
def tests():
# empty matrix
t1 = [[]]
o1 = [[]]
assert writeX(t1) == o1
# small matrix
t2 = [['x']]
o2 = [['x']]
assert writeX(t2) == o2
# simple matrix, one x
t3 = [['-','-'],
['-','x']]
o3 = [['-','x'],
['x','x']]
assert writeX(t3) == o3
# simple matrix, overlapping x's
t4 = [['-','x'],
['-','x']]
o4 = [['x','x'],
['x','x']]
assert writeX(t4) == o4
# simple matrix, multiple overlaps
t5 = [['x','-'],['-','x']]
o5 = [['x','x'],['x','x']]
assert writeX(t5) == o5
# big matrix
t6 = [['-','-','-','-','-'],
['-','-','-','-','-'],
['-','-','-','x','-'],
['x','-','-','-','-'],
['-','-','-','-','-']]
o6 = [['x','-','-','x','-'],
['x','-','-','x','-'],
['x','x','x','x','x'],
['x','x','x','x','x'],
['x','-','-','x','-']]
assert writeX(t6) == o6
# flat matrix
t7 = [['-','x']]
o7 = [['x','x']]
assert writeX(t7) == o7
tests()