MCPcopy Create free account
hub / github.com/careercup/ctci / MatrixProcessor

Class MatrixProcessor

python/Chapter 1/Question1_7/ChapQ1.7.py:4–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2#and column are set to 0.
3
4class MatrixProcessor:
5
6 def __init__(self, matrix):
7 self.n=len(matrix)
8 self.m=len(matrix[0])
9 self.matrix = matrix
10
11 def __str__(self):
12 rowstring=""
13 for row in self.matrix:
14 rowstring = rowstring + "["
15 for cell in row:
16 rowstring = rowstring + " " + str(cell) + " "
17 rowstring = rowstring + "]\n"
18 return rowstring
19
20 def zeroProcess(self):
21 columnstozero=[]
22 rowstozero=[]
23 for rowcount,row in enumerate(self.matrix):
24 for cellcount, cell in enumerate(row):
25 if cell == 0:
26 columnstozero.append(cellcount)
27 rowstozero.append(rowcount)
28 for rownum in rowstozero:
29 self.zeroRow(rownum)
30 for colnum in columnstozero:
31 self.zeroColumn(colnum)
32
33 def zeroRow(self, rownum):
34 newRow=[]
35 for x in range(0, self.m):
36 newRow.append(0)
37 self.matrix[rownum]=newRow
38
39 def zeroColumn(self, colnum):
40 for row in self.matrix:
41 row[colnum]=0
42
43#testing
44

Callers 1

ChapQ1.7.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected