Created
October 27, 2021 11:48
-
-
Save BelegCuthalion/ac45d7f4feb730ccdef3f7a16866f712 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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