def step_function(n):
    'Do a single step in the Kaprekar process'
    # http://en.wikipedia.org/wiki/6174_(number)
    s = format(n, '04d')        # 1253          --> '1253'
    t = ''.join(sorted(s))      # '1253'        --> '1235'
    u = t[::-1]                 # '1235'        --> '5321'
    return int(u) - int(t)      # (5321 - 1235) --> 4086
Do a single step in the Kaprekar process
recipes/Python/577749_Experiment_Kaprekars/recipe-577749.py