'Return the previous partition of the integer, n, in lexical order,
wrapping around to [1, ..., 1] if the partition is [n].
Examples
>>> from sympy.combinatorics.partitions import IntegerPartition
>>> p = IntegerPartition([4])
>>> print(p.prev_lex())
[3, 1]
>>> p.partition > p.prev_lex().partition
True'

def prev_lex(self):

    d = defaultdict(int)
    d.update(self.as_dict())
    keys = self._keys
    if (keys == [1]):
        return IntegerPartition({self.integer: 1})
    if (keys[(-1)] != 1):
        d[keys[(-1)]] -= 1
        if (keys[(-1)] == 2):
            d[1] = 2
        else:
            d[(keys[(-1)] - 1)] = d[1] = 1
    else:
        d[keys[(-2)]] -= 1
        left = (d[1] + keys[(-2)])
        new = keys[(-2)]
        d[1] = 0
        while left:
            new -= 1
            if ((left - new) >= 0):
                d[new] += (left // new)
                left -= (d[new] * new)
    return IntegerPartition(self.integer, d)
