The challenge
Given a sequence of numbers, find the largest pair sum in the sequence.
For example
[10, 14, 2, 23, 19] --> 42 (= 23 + 19)
[99, 2, 2, 23, 19] --> 122 (= 99 + 23)
Input sequence contains minimum two elements and every element is an integer.
The solution in Python code
Option 1:
def largest_pair_sum(numbers):
return sum(sorted(numbers)[-2:])
Option 2:
def largest_pair_sum(numbers):
max1 = max(numbers)
numbers.remove(max1)
max2 = max(numbers)
return max1 + max2
Option 3:
def largest_pair_sum(num):
return num.pop(num.index(max(num))) + max(num)
Test cases to validate our solution
import test
from solution import largest_pair_sum
@test.describe("Fixed Tests")
def fixed_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
test.assert_equals(largest_pair_sum([10,14,2,23,19]), 42)
test.assert_equals(largest_pair_sum([-100,-29,-24,-19,19]), 0)
test.assert_equals(largest_pair_sum([1,2,3,4,6,-1,2]), 10)
test.assert_equals(largest_pair_sum([-10, -8, -16, -18, -19]), -18)