Write down a sequence of positive integers that never decreases. The list can include duplicates. As an example, here's a list of primes:
2, 3, 5, 7, 11, 13
Call this increasing sequence of positive integers $$p_n$$ and call the largest integer in the sequence $$m$$. We now define the frequency sequence of $$p_n$$ as the sequence of $$m + 1$$ positive integers that records the number of members of $$p_n$$ that are less than 1, less than 2, and so on. For the list of primes above, the frequency sequence is
0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6
Pleasingly, the frequency sequence of the frequency sequence of $$p_n$$ is always equal to $$p_n$$ (except for one extra number $$m + 1$$ at the end of the sequence). That is, if we take the frequency sequence of the above frequency sequence, we again obtain the sequence
2, 3, 5, 7, 11, 13, 14
Now increase the numbers in each of these two sequences according to their position in the sequence — that is, add 1 to the first element of each, 2 to the second element, and so on. With the primes that gives us the following two increasing sequences of positive integers.
3, 5, 8, 11, 16, 19
1, 2, 4, 6, 7, 9, 10, 12, 13, 14, 15, 17, 18, 20
These two sequences will always be complementary — all positive integers appear, but they're split between the two sequences, without duplicates.
Implement these four functions that each take a sequence (list or tuple) of integers (int):
A function isincreasing that returns a Boolean value (bool) that indicates whether the integers in the given sequence are increasing. Duplicates are allowed (in other words: you don't have to check if the sequence is strictly increasing).
A function frequency_sequence that returns a new list containing the integers (int) in the frequency sequence of the given sequence. In case the given sequence is not increasing, the function must raise an AssertionError with the message given sequence is not increasing.
A function lift that returns a new list containing the integers (int) of the given sequence that have been increased according to their position in the sequence — that is, 1 added to the first integer in the given sequence, 2 to the second integer, and so on.
A function complementary_sequences that returns a tuple with two elements. The first element is a new list containing the integers (int) of the given sequence that have been increased according to their position in the sequence. The second element is a new list containing the integers (int) in the frequency sequence of the given sequence that have been increased according to their position in the sequence. In case the given sequence is not increasing, the function must raise an AssertionError with the message given sequence is not increasing.
>>> isincreasing([2, 3, 5, 7, 11, 13])
True
>>> isincreasing((0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6))
True
>>> isincreasing([5, 3, 2, 7, 8, 1, 9])
False
>>> frequency_sequence([2, 3, 5, 7, 11, 13])
[0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6]
>>> frequency_sequence((0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6))
[2, 3, 5, 7, 11, 13, 14]
>>> frequency_sequence([5, 3, 2, 7, 8, 1, 9])
Traceback (most recent call last):
AssertionError: given sequence is not increasing
>>> lift([2, 3, 5, 7, 11, 13])
[3, 5, 8, 11, 16, 19]
>>> lift((0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6))
[1, 2, 4, 6, 7, 9, 10, 12, 13, 14, 15, 17, 18, 20]
>>> lift([5, 3, 2, 7, 8, 1, 9])
[6, 5, 5, 11, 13, 7, 16]
>>> complementary_sequences([2, 3, 5, 7, 11, 13])
([3, 5, 8, 11, 16, 19], [1, 2, 4, 6, 7, 9, 10, 12, 13, 14, 15, 17, 18, 20])
>>> complementary_sequences((1, 3, 3, 5, 5, 5, 7, 7, 7, 7))
([2, 5, 6, 9, 10, 11, 14, 15, 16, 17], [1, 3, 4, 7, 8, 12, 13, 18])
>>> complementary_sequences([5, 3, 2, 7, 8, 1, 9])
Traceback (most recent call last):
AssertionError: given sequence is not increasing