Tuesday, March 1, 2022

Sum of Minimums! Python Codewars

Task:

Given a 2D ( nested ) list ( array, vector, .. ) of size m * n, your task is to find the sum of the minimum values in each row.

For Example:

[ [ 1, 2, 3, 4, 5 ]        #  minimum value of row is 1
, [ 5, 6, 7, 8, 9 ]        #  minimum value of row is 5
, [ 20, 21, 34, 56, 100 ]  #  minimum value of row is 20
]

So the function should return 26 because the sum of the minimums is 1 + 5 + 20 = 26.

Note: You will always be given a non-empty list containing positive values.

Solution:


def sum_of_minimums(numbers):
    res = 0
    for i in numbers:
        res += sorted(i)[0]
    return res

def sum_of_minimums(numbers):
    return sum(map(min, numbers))
    
print(sum_of_minimums([ [ 7,9,8,6,2 ], [6,3,5,4,3], [5,8,7,4,5] ]))

Sum of Minimums! Solutions | Codewars



Related Posts


No comments :

Post a Comment