diff options
| author | Packy Anderson <packy@cpan.org> | 2024-09-25 01:26:17 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-09-25 01:28:31 -0400 |
| commit | 6db7aa87619e0d1d6e05be63baa1ab49e60279d7 (patch) | |
| tree | 278c660e146a483886e0a6a232af2f4522d29ad8 /challenge-288/packy-anderson/python/ch-1.py | |
| parent | 9ced81d32e594001ffe943db9363bb48a4583d67 (diff) | |
| download | perlweeklychallenge-club-6db7aa87619e0d1d6e05be63baa1ab49e60279d7.tar.gz perlweeklychallenge-club-6db7aa87619e0d1d6e05be63baa1ab49e60279d7.tar.bz2 perlweeklychallenge-club-6db7aa87619e0d1d6e05be63baa1ab49e60279d7.zip | |
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
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") |
