List the first $$2n$$ positive integers, with $$n \in \mathbb{N}_0$$.
1, 2, 3, 4, 5, 6, 7, 8
Split these numbers arbitrarily into two groups of $$n$$ numbers, with half of the number going into the first group and the other half in the second group.
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 $$n^2$$. \[|1 - 8| + |4 - 5| + |6 - 3| + |7 - 2| = 7 + 1 + 3 + 5 = 16 = n^2\] This identity was proven by Vyacheslav Proizvolov.
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 $$n \in \mathbb{N}_0$$. In case the number does not meet the conditions, the function must raise an AssertionError with the message invalid length. In addition, the function has an optional second parameter different that may take a Boolean value (default value: True). The function must return a tuple that contains two groups of $$\frac{n}{2}$$ integers from the interval $$[1, n]$$. In case the value True is passed to the parameter different, the function must also make sure that all numbers across both groups are different.
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