Created
April 9, 2017 03:57
-
-
Save auscompgeek/a9dbd3b07c84f622ce54c76d9e458cb9 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
digits = '0123456789' | |
decrement_map = dict(zip(digits[1:], digits)) | |
def solve(N: str): | |
m = N[0] | |
for i, x in enumerate(N): | |
if m <= x: | |
m = max(m, x) | |
elif x == '0' and i == 1 and N[i-1] == '1': # 10... => 9... | |
return '9' * (len(N) - 1) | |
else: | |
return solve(N[:i-1] + decrement_map[N[i-1]]) + '9' * (len(N) - i) | |
return N | |
def main(): | |
T = int(input()) | |
for x in range(1, T+1): | |
print('Case #%d: %s' % (x, solve(input()))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment