PythonCodeFight-Day3

Day_3

Q8. matrixElementSum

After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost, and some of them are free, but there’s a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms.

Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don’t appear below a 0).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def matrixElementsSum(matrix):
# matrix = [[0, 1, 1, 2],
# [0, 5, 0, 0],
# [2, 0, 3, 3]]

for col in range(len(matrix[0])): # 열 검색
for row in range(len(matrix)): # 행 검색
if matrix[row][col] == 0: # 리스트에서 0의 인덱스 찾기
print("row: {} col: {} is zero".format(row,col)) # 0의 열 중 0의 행 아래로 다 0으로 처리
for remain in range(row,len(matrix),1):
matrix[remain][col] = 0

Temp_sum = list() # 전체 합 넣을 리스트
for row in range(len(matrix)):
Temp_sum.append(sum(matrix[row]))

return sum(Temp_sum)