diff options
Diffstat (limited to 'challenge-288/sgreen/python/ch-1.py')
| -rwxr-xr-x | challenge-288/sgreen/python/ch-1.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/challenge-288/sgreen/python/ch-1.py b/challenge-288/sgreen/python/ch-1.py new file mode 100755 index 0000000000..eff97cae4b --- /dev/null +++ b/challenge-288/sgreen/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import sys + + +def closest_palindrome(i: int) -> int: + # Negative integers always have a closest palindrome of 0 + if i < 0: + return 0 + + offset = 1 + while True: + check = i - offset + if str(check) == str(check)[::-1]: + return check + + check = i + offset + if str(check) == str(check)[::-1]: + return check + + offset += 1 + + +def main(): + # Convert input into integers + result = closest_palindrome(int(sys.argv[1])) + print(result) + + +if __name__ == '__main__': + main() |
