Skip to content

Instantly share code, notes, and snippets.

@BelegCuthalion
Created October 27, 2021 11:48
Show Gist options
  • Save BelegCuthalion/ac45d7f4feb730ccdef3f7a16866f712 to your computer and use it in GitHub Desktop.
Save BelegCuthalion/ac45d7f4feb730ccdef3f7a16866f712 to your computer and use it in GitHub Desktop.
def list2Int(l):
n = 0
for i in range(len(l)):
n += l[i] * (10 ** (len(l)-i-1))
return n
def next_smaller(n):
n = list(str(n))
n = [int(x) for x in n]
for s in range(2, len(n)+1):
c = len(n)-s
if n[c] > n[c+1]:
l = n[c:]
l.sort(reverse=True)
i = 0
while(l[i] >= n[c]):
i += 1
n[c] = l[i]
l = l[:i] + l[i+1:]
n[c+1:] = l
if(n[0] != 0):
return list2Int(n)
else:
break
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment