diff options
Diffstat (limited to 'challenge-288')
| -rw-r--r-- | challenge-288/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-288/laurent-rosenfeld/perl/ch-1.pl | 19 | ||||
| -rw-r--r-- | challenge-288/laurent-rosenfeld/raku/ch-1.raku | 12 |
3 files changed, 32 insertions, 0 deletions
diff --git a/challenge-288/laurent-rosenfeld/blog.txt b/challenge-288/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..e3b16725c7 --- /dev/null +++ b/challenge-288/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/09/perl-weekly-challenge-288-closest-palindrome.html diff --git a/challenge-288/laurent-rosenfeld/perl/ch-1.pl b/challenge-288/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..d2d709a504 --- /dev/null +++ b/challenge-288/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use feature 'say'; + +sub closest_palindrome { + my $in = shift; + my $i = 1; + while (1) { + return $in - $i if reverse($in - $i) eq $in - $i; + return $in + $i if reverse($in + $i) eq $in + $i; + $i++; + } +} + +my @tests = (123, 2, 1400, 1001); +for my $test (@tests) { + printf "%-6d => ", $test; + say closest_palindrome $test; +} diff --git a/challenge-288/laurent-rosenfeld/raku/ch-1.raku b/challenge-288/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..c00c271c2a --- /dev/null +++ b/challenge-288/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,12 @@ +sub closest-palindrome ($in) { + for 1..Inf -> $i { + return $in - $i if ($in - $i).flip eq $in - $i; + return $in + $i if ($in + $i).flip eq $in + $i; + } +} + +my @tests = 123, 2, 1400, 1001; +for @tests -> $test { + printf "%-6d => ", $test; + say closest-palindrome $test; +} |
