Skip to content

Latest commit

 

History

History
21 lines (16 loc) · 667 Bytes

count_of_positives_sum_of_negatives.md

File metadata and controls

21 lines (16 loc) · 667 Bytes

Description

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].

My Solution

def count_positives_sum_negatives(lst)
  return [] if lst.empty?
  positive, negative = lst.partition(&:positive?)
  [positive.count, negative.sum]
end