Created
January 14, 2015 13:12
-
-
Save prat0318/abdd05994781e6712cff to your computer and use it in GitHub Desktop.
Cooking The Books - https://www.facebook.com/hackercup/problems.php?pid=582062045257424&round=742632349177460
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
times = int(raw_input()) | |
for i in xrange(times): | |
num_str = raw_input() | |
num = list(num_str) | |
old_num = list(num) | |
for j in xrange(len(num) - 1): | |
k = j + 1 | |
if max(num[k:]) > num[j]: | |
k1 = k + num_str[k:].rfind(max(num[k:])) | |
num[j], num[k1] = num[k1], num[j] | |
break | |
max_num = ''.join(num) | |
num = old_num | |
for j in xrange(len(num) - 1): | |
k = j + 1 | |
if min(num[k:]) < num[j]: | |
min_dig = min(num[k:]) | |
if j == 0 and min_dig == '0': | |
non_zero = [x for x in num[k:] if x != '0'] | |
if non_zero == [] or min(non_zero) >= num[j]: | |
continue | |
min_dig = min(non_zero) | |
k1 = k + num_str[k:].rfind(min_dig) | |
num[j], num[k1] = num[k1], num[j] | |
break | |
min_num = ''.join(num) | |
print "Case #{0}: {1} {2}".format(str(i + 1), min_num, max_num) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment