overactive

Untitled

Nov 10th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. from itertools import permutations
  2. def next_smaller(num):
  3.     num = str(num)
  4.     l = len(num)
  5.     cur = num[-1]
  6.     indx = 0
  7.     out = []
  8.  
  9.     for n in num[::-1]:
  10.         if n <= cur:
  11.             cur = n
  12.             indx +=1
  13.         else:
  14.             break
  15.    
  16.     res = (num[-indx-1::])
  17.     for var in list(permutations(res)):
  18.         out.append(int(''.join(var)))
  19.  
  20.  
  21.     out.sort()
  22.     res = int(res)
  23.     i = out.index(res)
  24.     if i == 0:
  25.         return -1
  26.  
  27.     else:
  28.         return int(num[:l-indx-1] + str(out[i-1]))
Advertisement
Add Comment
Please, Sign In to add comment