Last active
July 23, 2017 08:31
-
-
Save amraks/04cb9875d462b0468dd1c9d7ddd060f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 find_local_max(arr, val): | |
local_max = None | |
local_max_indx = None | |
for i, v in enumerate(arr): | |
if not local_max and v < val: | |
local_max = v | |
local_max_indx = i | |
elif v > local_max and v < val: | |
local_max = v | |
local_max_indx = i | |
return local_max, local_max_indx | |
def func(n): | |
prev = None | |
cur = None | |
digits = [] | |
remaining = [] | |
switching_point = None | |
while n: | |
prev = cur | |
cur = n % 10 | |
n /= 10 | |
if not prev or cur <= prev: | |
digits.append(cur) | |
continue | |
else: | |
while n: | |
remaining.append(n % 10) | |
n /= 10 | |
switching_point = cur | |
break | |
if not switching_point: | |
print('none available') | |
return | |
local_max, local_max_indx = find_local_max(digits, switching_point) | |
remaining.reverse() | |
remaining.append(local_max) | |
digits = digits[0:local_max_indx] + digits[local_max_indx + 1:] | |
digits = sorted(digits + [switching_point], reverse=True) | |
print(int(''.join(map(str, remaining + digits)))) | |
func(41732) | |
func(98764) | |
func(128728) | |
func(126) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment