Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations
- def next_smaller(num):
- num = str(num)
- l = len(num)
- cur = num[-1]
- indx = 0
- out = []
- for n in num[::-1]:
- if n <= cur:
- cur = n
- indx +=1
- else:
- break
- res = (num[-indx-1::])
- for var in list(permutations(res)):
- out.append(int(''.join(var)))
- out.sort()
- res = int(res)
- i = out.index(res)
- if i == 0:
- return -1
- else:
- return int(num[:l-indx-1] + str(out[i-1]))
Advertisement
Add Comment
Please, Sign In to add comment