aboutsummaryrefslogtreecommitdiff
path: root/challenge-206/lubos-kolouch/python/ch-2.py
blob: c967a3503e0b7b4d021cb2c30905d217728ac9d2 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import List


def max_min_pair_sum(array: List[int]) -> int:
    # Sort the array in ascending order
    sorted_array = sorted(array)

    # Use pairwise iterator to get pairs of adjacent elements
    # (0,1), (2,3), (4,5), ...
    pairwise_array = [(sorted_array[i], sorted_array[i + 1])
                      for i in range(0, len(sorted_array), 2)]

    # Calculate the sum of the minimum of each pair
    return sum(min(pair) for pair in pairwise_array)


# Define test cases
test1 = [1, 2, 3, 4]
expected1 = 4
test2 = [0, 2, 1, 3]
expected2 = 2

# Run tests
assert max_min_pair_sum(test1) == expected1
assert max_min_pair_sum(test2) == expected2