简体   繁体   中英

next biggest number with same digits

my code seems right, but i need it to return -1 if no bigger number can be generated:

def next_bigger(n):
    strNum = str(n)
    length = len(strNum)
    for i in range(length-2, -1, -1):
        current = strNum[i]
        right = strNum[i+1]
        if current < right:
            temp = sorted(strNum[i:])
            next = temp[temp.index(current) + 1]
            temp.remove(next)
            temp = ''.join(temp)    
            return int(strNum[:i] + next + temp)
        else: 
            return -1
    return n

My attempt to solve this isn't working: adding the else is what I percevied to be the alternative to when current is greater than right .

Please help!

Anyway, the flow of your code is wrong: in your loop, you have the following structure:

for A :
    if B :
        return
    else :
        return

So your program will always terminate before a second iteration.

In some cases fixing the code is much harder than rewritting it. I am not sure of how much help this is to you but try the following code.

def next_bigger(n):
    str_num = str(n)
    size = len(str_num)
    for i in range(2, size + 1):
        sublist = list(str_num[-i:size])
        temp = sorted(sublist, reverse=True)
        if sublist != temp:
            return int(str_num[:size-i] + ''.join(temp))
    return -1

What it does is that it slices the number from the back (starting with 2 element slices and going on up to len ) and checks to see if the generated slice produces the biggest number possible when joined . If not it gets replaced with the next bigger and returns it. Let me know if this worked for you.


EXAMPLE

n = 4181536841

sublist = ['4', '1']
temp = ['4', '1']  # they are the same so no larger number can be produced just by looking at a slice of length 2.

#---------iteration 2---------------
sublist = ['8', '4', '1']
temp = ['8', '4', '1']  # they are again the same so no larger number can be produced just by looking at a slice of length 3.

#---------iteration 3---------------
sublist = ['6', '8', '4', '1']
temp = ['8', '6', '4', '1']  # now they are different. So produce a number out of temp (8641) ans stich it to the rest of the number (418153)
return 4181538641

Ev. Kounis is wrong. In eg next biggest after 4181536841 is 4181538146, not 4181538641.

Logic works like this:

1 - Find where list[-n] > list[-n - 1]
2 - find next number higher than list[-n] after list[-n]
3 - Switch next highest and list[-n - 1]
4 - reorder the remaining numbers after list[-n] from low to high

e.g. 29357632:
1 - list[-n] = 7 --> 2935[7]632
    since 7 > 5

2 - next highest number after list[-n] (7) is the 6 --> 29357[6]32

3 - switch list[-n-1] and next highest number (switch 5 and 6) --> 
293[6]7[5]32

4 - reorder rest of digits after list[-n-1] (6) --> 2936[7532] > 29362357

so next highest number after 29357632 is 29362357

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM