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.
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.
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$$.
>>> 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]