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 the counting numbers appear, but they're split between the two sequences, without duplicates.

Assignment

Implement the next four functions that each take a sequence (list or tuple) of integers (int):

Example

>>> increasing([2, 3, 5, 7, 11, 13])
True
>>> increasing((0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6))
True
>>> increasing([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

Resources