'Returns the next permutation in lexicographical order.
If self is the last permutation in lexicographical order
it returns None.
See [4] section 2.4.
Examples
>>> from sympy.combinatorics.permutations import Permutation
>>> p = Permutation([2, 3, 1, 0])
>>> p = Permutation([2, 3, 1, 0]); p.rank()
17
>>> p = p.next_lex(); p.rank()
18
See Also
rank, unrank_lex'

def next_lex(self):

    perm = self.array_form[:]
    n = len(perm)
    i = (n - 2)
    while (perm[(i + 1)] < perm[i]):
        i -= 1
    if (i == (-1)):
        return None
    else:
        j = (n - 1)
        while (perm[j] < perm[i]):
            j -= 1
        (perm[j], perm[i]) = (perm[i], perm[j])
        i += 1
        j = (n - 1)
        while (i < j):
            (perm[j], perm[i]) = (perm[i], perm[j])
            i += 1
            j -= 1
    return self._af_new(perm)
