Hi guys, i’m trying to solve this problem but for some reason it’s returning list out of index…
the problem is
A building is represented by a rectangular matrix of rooms, each cell containing an integer - the price of the room. Some rooms are free (their cost is 0), but that’s probably because they are haunted. That is why any room that is free or is located anywhere below a free room in the same column is not considered suitable.
Calculate the total price of all the rooms that are suitable.
[[0, 1, 1, 2], ----- [0, 5, 0, 0], ----- [2, 0, 3, 3]]
the output should be
matrixElementsSum(matrix) = 9.
My code is:
def matrixElementsSum(matrix): floors = len(matrix) current_floor = floors current_tower = 0 apartments = len(matrix) total = 0 #while we have floors above and apartments in the array... while(current_floor-1>=0 and current_tower+1<=apartments): #if this apartment = 0, we go to the next tower if(matrix[current_floor-1][current_tower]==0): current_tower = current_tower+1 current_floor = floors #if not, we sum it to total price and go up else: total = total + (matrix[current_floor][current_tower]) current_floor = current_floor-1 return total
result: index out of range