forked from dmitriy-kiriyenko/homework0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.rb
More file actions
31 lines (29 loc) · 807 Bytes
/
Copy pathtask1.rb
File metadata and controls
31 lines (29 loc) · 807 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
# Takes an array of integers as an argument
# and returns the sum of its elements.
# For an empty array it should return zero.
def sum(array)
array.inject(0) { |r, e| r + e }
end
# Takes an array of integers as an argument
# and returns the sum of its two largest elements.
# For an empty array it should return zero.
# For an array with just one element,
# it should return that element.
def max_2_sum(array)
new = []
while new.size < 2 && array.size != 0
array.sort!
new << array.max
array.pop
end
sum new
end
# DIFFICULT
# Takes an array of integers
# and an additional integer, n, as arguments
# and returns true
# if any two elements in the array of integers sum to n.
# An empty array should sum to zero by definition.
def sum_to_n?(array, n)
raise "Not yet implemented"
end