aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-27 09:05:37 +0100
committerGitHub <noreply@github.com>2021-05-27 09:05:37 +0100
commitf22a4886e6299567f39fa370e9be13db3ae4dde5 (patch)
tree423aadf5a6974943ed81c1ce268443ca04951880
parentc60a02b90df3d66d63b69db3ade3dafdf40f0db1 (diff)
parent5fa1257b05397b05cdb77788a017347f38835d62 (diff)
downloadperlweeklychallenge-club-f22a4886e6299567f39fa370e9be13db3ae4dde5.tar.gz
perlweeklychallenge-club-f22a4886e6299567f39fa370e9be13db3ae4dde5.tar.bz2
perlweeklychallenge-club-f22a4886e6299567f39fa370e9be13db3ae4dde5.zip
Merge pull request #4151 from polettix/polettix/pwc114
Add polettix's solution to challenge-114
-rw-r--r--challenge-114/polettix/blog.txt1
-rw-r--r--challenge-114/polettix/blog1.txt1
-rw-r--r--challenge-114/polettix/perl/ch-1.pl29
-rw-r--r--challenge-114/polettix/perl/ch-2.pl17
-rw-r--r--challenge-114/polettix/raku/ch-1.raku28
-rw-r--r--challenge-114/polettix/raku/ch-2.raku16
6 files changed, 92 insertions, 0 deletions
diff --git a/challenge-114/polettix/blog.txt b/challenge-114/polettix/blog.txt
new file mode 100644
index 0000000000..b5e34e5793
--- /dev/null
+++ b/challenge-114/polettix/blog.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2021/05/26/pwc114-next-palindrome-number/
diff --git a/challenge-114/polettix/blog1.txt b/challenge-114/polettix/blog1.txt
new file mode 100644
index 0000000000..e52bd77988
--- /dev/null
+++ b/challenge-114/polettix/blog1.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2021/05/27/pwc114-higher-integer-set-bits/
diff --git a/challenge-114/polettix/perl/ch-1.pl b/challenge-114/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..9329e4ba94
--- /dev/null
+++ b/challenge-114/polettix/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use 5.024;
+use warnings;
+use experimental qw< postderef signatures >;
+no warnings qw< experimental::postderef experimental::signatures >;
+
+sub next_palindrome_number ($N) {
+ my $l = length $N;
+ return '1' . ('0' x ($l - 1)) . '1' unless $N =~ m{[0-8]}mxs;
+ my $n = substr $N, 0, $l / 2;
+ my $mid = $l % 2 ? substr($N, $l / 2, 1) : '';
+
+ # just try to build straight from the inputs...
+ if ((my $candidate = $n . $mid . reverse($n)) > $N) {
+ return $candidate;
+ }
+
+ # if there's a "$mid", try increasing that
+ if ($l % 2) {
+ return $n . ($mid + 1) . reverse($n) if $mid != 9;
+ $mid = 0;
+ }
+
+ ++$n;
+ return $n . $mid . reverse($n);
+}
+
+@ARGV = (1234) unless @ARGV;
+say next_palindrome_number($_) for @ARGV;
diff --git a/challenge-114/polettix/perl/ch-2.pl b/challenge-114/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..07c7d74298
--- /dev/null
+++ b/challenge-114/polettix/perl/ch-2.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl
+use 5.024;
+use warnings;
+use experimental qw< postderef signatures >;
+no warnings qw< experimental::postderef experimental::signatures >;
+
+sub higher_integer_set_bits ($N) {
+ sub n_bits ($x) { sprintf('%b', $x) =~ tr/1/1/ };
+ my $initial = n_bits($N);
+ while ('necessary') {
+ ++$N;
+ return $N if $initial == n_bits($N);
+ }
+}
+
+@ARGV = 3 unless @ARGV;
+say higher_integer_set_bits($_) for @ARGV;
diff --git a/challenge-114/polettix/raku/ch-1.raku b/challenge-114/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..23a678aa2e
--- /dev/null
+++ b/challenge-114/polettix/raku/ch-1.raku
@@ -0,0 +1,28 @@
+#!/usr/bin/env raku
+use v6;
+
+sub next-palindrome-number (IntStr $N) {
+ my $l = $N.chars;
+ return '1' ~ ('0' x ($l - 1)) ~ '1' unless $N ~~ m{<[0 .. 8]>};
+ my $n = $N.substr(0, $l / 2);
+ my $mid = $l % 2 ?? $N.substr($l / 2, 1) !! '';
+
+ # just try to build straight from the inputs...
+ if (my $candidate = $n ~ $mid ~ $n.flip) > $N {
+ return $candidate;
+ }
+
+ # if there's a "$mid", try increasing that
+ if ($l % 2) {
+ return $n ~ ($mid + 1) ~ $n.flip if $mid < 9;
+ $mid = 0;
+ }
+
+ ++$n;
+ return $n ~ $mid ~ $n.flip;
+}
+
+sub MAIN (*@inputs is copy) {
+ @inputs.push(1234) unless @inputs.elems;
+ next-palindrome-number($_).say for @inputs;
+}
diff --git a/challenge-114/polettix/raku/ch-2.raku b/challenge-114/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..a02224e675
--- /dev/null
+++ b/challenge-114/polettix/raku/ch-2.raku
@@ -0,0 +1,16 @@
+#!/usr/bin/env raku
+use v6;
+
+sub higher-integer-set-bits (Int $N is copy) {
+ sub n-bits ($x) { ($x.base(2) ~~ m:g/1/).elems };
+ my $initial = n-bits($N);
+ while True {
+ ++$N;
+ return $N if $initial == n-bits($N);
+ }
+}
+
+sub MAIN (*@inputs is copy) {
+ @inputs.push(3) unless @inputs.elems;
+ higher-integer-set-bits($_).say for @inputs;
+}