-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_sum.rb
More file actions
31 lines (24 loc) · 743 Bytes
/
count_sum.rb
File metadata and controls
31 lines (24 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 8kyu Count of positives / sum of negatives
def count_positives_sum_negatives(lst)
if !lst || lst.length() == 0
return []
else
positives = []
negatives = []
lst.each do |el|
el > 0 ? positives.push(el) : negatives.push(el)
end
positive_count = positives.length() || 0
negative_sum = negatives.reduce(:+) || 0
[positive_count, negative_sum]
end
end
# Second solution
def count_positives_sum_negatives(lst)
return [] if !lst || lst.empty?
[lst.count(&:positive?) || 0, lst.select(&:negative?).reduce(:+)|| 0]
end
# Refactor to use ternary operator
def count_positives_sum_negatives(lst)
!lst || lst.empty? ? [] : [lst.count(&:positive?) || 0, lst.select(&:negative?).reduce(:+)|| 0]
end