diff options
Diffstat (limited to 'challenge-288/packy-anderson/python/ch-1.py')
| -rwxr-xr-x | challenge-288/packy-anderson/python/ch-1.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-288/packy-anderson/python/ch-1.py b/challenge-288/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..f19780b1a8 --- /dev/null +++ b/challenge-288/packy-anderson/python/ch-1.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +def isPalindrome(num): + # convert numerics to Strings, then reverse one of them + return str(num) == str(num)[::-1] + +def closestPalindrome(string): + num = int(string) + distance = 1 + while True: + # is the smaller number at this distance a palindrome? + if isPalindrome(num - distance): + return str(num - distance) + + # is the larger number at this distance a palindrome? + if isPalindrome(num + distance): + return str(num + distance) + + # step 1 number futher away + distance += 1 + +def solution(string): + print(f'Input: $str = "{string}"') + print(f'Output: "{closestPalindrome(string)}"') + +print('Example 1:') +solution("123") + +print('\nExample 2:') +solution("2") + +print('\nExample 3:') +solution("1400") + +print('\nExample 4:') +solution("1001") + +print('\nExample 5: (it doesn\'t say the input is a POSITIVE int)') +solution("-1") |
