Code Signal_练习题_matrixElementsSum

After they became famous, the CodeBots all decided to move to a new building and live together. The building is represented by a rectangular matrix of rooms. Each cell in the matrix contains an integer that represents the price of the room. Some rooms are free(their cost is 0), but that‘s probably because they are haunted, so all the bots are afraid of them. That is why any room that is free or is located anywhere below a free room in the same column is not considered suitable for the bots to live in.

Help the bots calculate the total price of all the rooms that are suitable for them.

Example

  • For
matrix = [[0, 1, 1, 2],
          [0, 5, 0, 0],
          [2, 0, 3, 3]]

the output should be
matrixElementsSum(matrix) = 9.

Here‘s the rooms matrix with unsuitable rooms marked with ‘x‘:

[[x, 1, 1, 2],
 [x, 5, x, x],
 [x, x, x, x]]

Thus, the answer is 1 + 5 + 1 + 2 = 9.

  • For
matrix = [[1, 1, 1, 0],
          [0, 5, 0, 1],
          [2, 1, 3, 10]]

the output should be
matrixElementsSum(matrix) = 9.

Here‘s the rooms matrix with unsuitable rooms marked with ‘x‘:

[[1, 1, 1, x],
 [x, 5, x, x],
 [x, 1, x, x]]

Note that the free room in the first row make the full column unsuitable for bots.

Thus, the answer is 1 + 1 + 1 + 5 + 1 = 9.

我的解答:

 1 def matrixElementsSum(matrix):
 2     num = 0
 3     j = 0
 4     while j<len(matrix):
 5         num = num + sum(matrix[j])
 6         for i in range(len(matrix[j])):
 7             if matrix[j][i] == 0:
 8                 for x in range(j+1,len(matrix)):
 9                     matrix[x][i] = 0
10         j += 1
11     return num

膜拜大佬:

 1 def matrixElementsSum(m):
 2     r = len(m)
 3     c = len(m[0])
 4     total=0
 5     for j in range(c):
 6         for i in range(r):
 7             if m[i][j]!=0:
 8                 total+=m[i][j]
 9             else:
10                 break
11     return total

原文地址:https://www.cnblogs.com/YD2018/p/9348418.html

时间: 2024-10-11 06:26:45

Code Signal_练习题_matrixElementsSum的相关文章

Code Signal_练习题_shapeArea

A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting

Code Signal_练习题_All Longest Strings

Given an array of strings, return another array containing all of its longest strings. Example For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should beallLongestStrings(inputArray) = ["

Code Signal_练习题_isLucky

Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half. Given a ticket number n, determine if it's lucky or not. Example For n

Code Signal_练习题_commonCharacterCount

Given two strings, find the number of common characters between them. Example For s1 = "aabcc" and s2 = "adcaa", the output should becommonCharacterCount(s1, s2) = 3. Strings have 3 common characters - 2 "a"s and 1 "c&qu

Code Signal_练习题_Are Similar?

Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays. Given two arrays a and b, check whether they are similar. Example For a = [1, 2, 3] and b = [1, 2, 3], the output should

Code Signal_练习题_Minesweeper

In the popular Minesweeper game you have a board with some mines and those cells that don't contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to c

Code Signal_练习题_variableName

Correct variable names consist only of English letters, digits and underscores and they can't start with a digit. Check if the given string is a correct variable name. Example For name = "var_1__Int", the output should bevariableName(name) = tru

Code Signal_练习题_chessBoardCellColor

Given two cells on the standard chess board, determine whether they have the same color or not. Example For cell1 = "A1" and cell2 = "C3", the output should bechessBoardCellColor(cell1, cell2) = true. For cell1 = "A1" and cel

Code Signal_练习题_alphabeticShift

Given a string, replace each its character by the next one in the English alphabet (z would be replaced by a). Example For inputString = "crazy", the output should bealphabeticShift(inputString) = "dsbaz". 我的解答: def alphabeticShift(inp