Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Parting the number
- def num_parts(x):
- if x == 0:
- return []
- last_number = x%10
- x = (x-last_number)/10
- numar = [last_number]
- numar.extend(num_parts(x))
- return numar
- # Joining it together
- def join_num(arr):
- num = 0
- for i in arr[::-1]:
- num = num*10
- num += i
- return num
- # Finds the next big number // Switching 2 of the parts
- def next_bigger(x):
- arr = num_parts(x)
- for i in range(len(arr)):
- if i+1 >= len(arr):
- return -1
- if arr[i] > arr[i+1]:
- arr[i], arr[i+1] = arr[i+1], arr[i]
- return join_num(arr)
- return -1
Advertisement
Add Comment
Please, Sign In to add comment