Skip to content

Instantly share code, notes, and snippets.

@prat0318
Created January 14, 2015 13:12
Show Gist options
  • Save prat0318/abdd05994781e6712cff to your computer and use it in GitHub Desktop.
Save prat0318/abdd05994781e6712cff to your computer and use it in GitHub Desktop.
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