-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixElementsSum.py
74 lines (49 loc) · 2.35 KB
/
matrixElementsSum.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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).
# Example
# For
# matrix = [[0, 1, 1, 2],
# [0, 5, 0, 0],
# [2, 0, 3, 3]]
# the output should be
# matrixElementsSum(matrix) = 9.
# example 1
# There are several haunted rooms, so we'll disregard them as well as any rooms beneath them. 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.
# example 2
# Note that the free room in the final column makes the full column unsuitable for bots (not just the room directly beneath it). Thus, the answer is 1 + 1 + 1 + 5 + 1 = 9.
# Input/Output
# [execution time limit] 4 seconds (py3)
# [input] array.array.integer matrix
# A 2-dimensional array of integers representing the cost of each room in the building. A value of 0 indicates that the room is haunted.
# Guaranteed constraints:
# 1 ≤ matrix.length ≤ 5,
# 1 ≤ matrix[i].length ≤ 5,
# 0 ≤ matrix[i][j] ≤ 10.
# [output] integer
# The total price of all the rooms that are suitable for the CodeBots to live in.
def matrixElementsSum(matrix):
nocol = []
res = 0
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if j not in nocol:
if matrix[i][j] == 0:
nocol.append(j)
else:
res += matrix[i][j]
print(nocol, res)
return res
# for room in matrix[0]:
# if room == 0:
# nocol.append(matrix[0].index(room))
# for row in matrix:
# for col in row:
# if col.index(row) in nocol:
# continue
# if col == 0: