From 6db7aa87619e0d1d6e05be63baa1ab49e60279d7 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Wed, 25 Sep 2024 01:26:17 -0400 Subject: Challenge 288 solutions by Packy Anderson * Raku that maybe looks like Raku, but mostly like Perl * Perl * Python that definitely looks like Perl * Elixir solution for Task 1 (for now) 1 Blog post --- challenge-288/packy-anderson/python/ch-1.py | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 challenge-288/packy-anderson/python/ch-1.py (limited to 'challenge-288/packy-anderson/python/ch-1.py') 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") -- cgit