The algorithm for constructing a Kaprekar series was discovered in 1949 by the Indian mathematical Dattaraya Ramachandra Kaprekar. It was originally described for 4-digit numbers, but can easily be generalized for $$k$$-digit numbers.

Kaprekar series
Graphical representation of the number of steps required to end the Kaprekar series with a value that is equal to the next value, for the starting numbers $$n = 0, 1, \ldots, 9999$$ distributed over rows of length 100. Numbers with fewer than four digits are supplemented with leading zeroes, so that the Kaprekar series converges to 6174 for all values $$n$$.

The Kaprekar series for a number $$n$$ (int) is represented as a list whose first element is $$n$$. The next number (int) is calculated as the difference $$K(n) = n' - n''$$ between the number $$n'$$ that exists of the digits of $$n$$ sorted in descending order, and the number $$n''$$ that exists of the digits of $$n$$ sorted in ascending order. This next number is appended to the end of the list.

We keep on repeating this procedure with the last number in the list, until the difference is 0, is equal to the previous number or already occurs in the list. If the procedure results in a number that already occurs in the list, this number must not be appended to the list.

Assignment

Write a function kaprekar_series that takes a number $$n \in \mathbb{N}$$ (int). The function must return the Kaprekar series (list) for the given number $$n$$.

Example

>>> kaprekar_series(677)
[677, 99, 0]
>>> kaprekar_series(9876)
[9876, 3087, 8352, 6174]
>>> kaprekar_series(55500)
[55500, 54945, 50985, 92961, 86922, 75933, 63954, 61974, 82962]