Thursday, February 3, 2022

Count of positives / sum of negatives Codewars Python

Given an array of integers.

Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. 0 is neither positive nor negative.

If the input array is empty or null, return an empty array.

Example

For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65].


def count_positives_sum_negatives(arr):
    if not arr: return []
    count = 0
    sum_list = 0
    for x in arr:
        if x > 0:
            count += 1
        if x < 0:
            sum_list += x
    return [count,sum_list]

def count_positives_sum_negatives(arr):
    return [len([x for x in arr if x > 0])] + [sum(y for y in arr if y < 0)] if arr else []

Training on Count of positives / sum of negatives | Codewars



Related Posts


No comments :

Post a Comment