본문 바로가기
나는 이렇게 학습한다/Algorithm & SQL

0930. Count of positives, sum of negatives

by daco2020 2022. 9. 30.

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 is an empty array or is 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].



Solution:

def sorted_arr(func):
    def wrapper(arr):
        if not arr:
            return []
        arr = sorted([i for i in arr if i != 0], reverse=True)
        return func(arr)
    return wrapper

@sorted_arr
def count_positives_sum_negatives(arr):
    idx = len(arr)
    for i, e in enumerate(map(str, arr)):
        if e[0] == "-":
            idx = i
            break
    return [len(arr[:idx]), sum(arr[idx:])]


'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

1002. Twice as old  (0) 2022.10.03
1001. Hello, Name or World!  (0) 2022.10.02
0929. Summing a number's digits  (0) 2022.09.29
0928. Grasshopper - Terminal game combat function  (0) 2022.09.28
0927. Opposites Attract  (0) 2022.09.27