Skip to main content
1 of 6
mrakosms
  • 113
  • 1
  • 6

Next bigger number with the same digits

I'm new in python and develop.

You have to create a function that takes a positive integer number and returns the next bigger number formed by the same digits: 59853 -> 83559

def next_bigger(num):
numlist = [int(i) for i in str(num)]

index_of_replace_num = -1
i = len(numlist) - 1
while i > 0:
    if numlist[i] > numlist[i-1]:
        index_of_replace_num = i - 1
        break
    i -= 1

if index_of_replace_num == -1:
    return -1
else:
    firstlist = numlist[:index_of_replace_num]
    replaced_num = numlist[index_of_replace_num]
    secondlist = numlist[index_of_replace_num+1:]
    new_replaced_num = 9
    i = 0
    delindex = 0
    while i < len(secondlist):
        if secondlist[i] > replaced_num and secondlist[i] < new_replaced_num:
            new_replaced_num = secondlist[i]
            delindex = i
        i += 1

    secondlist.pop(delindex)
    secondlist.append(replaced_num)
    firstlist.append(new_replaced_num)
    output = firstlist + sorted(secondlist)
    return int(''.join(str(x) for x in output))

Please let me know if there are any better solutions. I would like feedback how to improve this. Thanks.

mrakosms
  • 113
  • 1
  • 6