Notice
Recent Posts
Recent Comments
«   2024/03   »
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
Tags
more
Archives
Today
Total
관리 메뉴

CHIqueen

[Intro] matrixElementsSum 본문

프로그래밍/CodeSignal

[Intro] matrixElementsSum

CHIqueen 2020. 3. 11. 12:40

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(m):
    r = len(m)
    c = len(m[0])
    total=0
    for j in range(c):
        for i in range(r):
            if m[i][j]!=0:
                total+=m[i][j]
            else:
                break
    return total

'프로그래밍 > CodeSignal' 카테고리의 다른 글

[Intro] commonCharacterCount  (0) 2020.03.11
[Intro] All Longest Strings  (0) 2020.03.11
[Intro] shapeArea  (0) 2020.03.03
[Intro] Make Array Consecutive 2  (0) 2020.03.03
[Intro] adjacentElementsProduct  (0) 2020.03.03
Comments