from itertools import permutations
def next_bigger(n):
nlist = list(str(n))
perm = permutations(nlist,len(str(n)))
perm = set(perm)
listofperm = []
nlist = int("".join(nlist))
for x in perm:
form = int("".join(x))
if form < nlist:
continue
else:
listofperm.append(form)
listofperm = sorted(listofperm)
if (listofperm.index(n) + 1) == len(listofperm):
indexofn = listofperm.index(n)
else:
indexofn = listofperm.index(n) + 1
return listofperm[indexofn]
I'm trying to get the next bigger number by rearranging the digits of n
but my code is very inefficient can you suggest ways to make my code more efficient and faster?
Inputs would be any integer.