[Python 문제풀이] 3일차

2020. 7. 11. 11:0003. Resources/Python

728x90
반응형

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).

    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)
반응형

'03. Resources > Python' 카테고리의 다른 글

[Python 문제풀이] 5일차  (0) 2020.07.13
[Python 문제풀이] 4일차  (0) 2020.07.12
[Python 문제풀이] 2일차  (0) 2020.07.10
[Python 문제풀이] 1일차  (0) 2020.07.09
파이썬 문제풀이 했던 것 업로드  (0) 2020.07.09