List the first
1, 2, 3, 4, 5, 6, 7, 8
Split these numbers arbitrarily into two
groups of
7, 1, 6, 4 2, 8, 5, 3
Arrange the numbers in the first group in ascending order, and the numbers in the second group in descending order.
1, 4, 6, 7 8, 5, 3, 2
Now if you compute the sum of the absolute
values of the difference between each pair of numbers at corresponding
positions in both groups, the sum will always equal
In this assignment we represent a group of numbers as a tuple of integers. Your task:
Write a function split
that takes an even integer
Write a function add that takes two groups of numbers. In case both groups do not have the same length, the function must raise an AssertionError with the message groups must have equal length. Otherwose, the function must return the sum of the absolute values of the difference between each pair of numbers at corresponding positions in both groups, after the numbers in the first group have been arranged in ascending order, and those in the second group in descending order.
>>> split(8)
((7, 1, 6, 4), (2, 8, 5, 3))
>>> split(8, different=False)
((4, 4, 7, 3), (7, 1, 2, 3))
>>> split(3)
Traceback (most recent call last):
AssertionError: invalid length
>>> split(-3, different=True)
Traceback (most recent call last):
AssertionError: invalid length
>>> add((7, 1, 6, 4), (2, 8, 5, 3))
16
>>> add((4, 4, 7, 3), (7, 1, 2, 3))
13
>>> add((8, 3, 15), (27, 24, 5, 42))
Traceback (most recent call last):
AssertionError: groups must have equal length