aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-16 20:08:27 +0000
committerGitHub <noreply@github.com>2024-02-16 20:08:27 +0000
commit7f565e10804ed0621cbd2818c1176c24caf2d2fc (patch)
treeb194292b55ca4d74d120116e3219dad20b79d13f
parent8746b9c7b09a0a3f2fb0dffcd68af9acc3579c74 (diff)
parent3b50c08c0360f94c25df0e78581b0333d07edf4d (diff)
downloadperlweeklychallenge-club-7f565e10804ed0621cbd2818c1176c24caf2d2fc.tar.gz
perlweeklychallenge-club-7f565e10804ed0621cbd2818c1176c24caf2d2fc.tar.bz2
perlweeklychallenge-club-7f565e10804ed0621cbd2818c1176c24caf2d2fc.zip
Merge pull request #9594 from wlmb/challenges
Add alternative solution
-rwxr-xr-xchallenge-256/wlmb/perl/ch-1.pl2
-rwxr-xr-xchallenge-256/wlmb/perl/ch-1a.pl19
2 files changed, 20 insertions, 1 deletions
diff --git a/challenge-256/wlmb/perl/ch-1.pl b/challenge-256/wlmb/perl/ch-1.pl
index 4163c91776..f5da52e2e6 100755
--- a/challenge-256/wlmb/perl/ch-1.pl
+++ b/challenge-256/wlmb/perl/ch-1.pl
@@ -7,7 +7,7 @@ use v5.36;
use List::Util qw(sum0);
die <<~"FIN" unless @ARGV;
Usage: $0 S1 [S2...]
- to pair Si with the reverse SJ and count the resulting pairs
+ to pair Si with the reverse Sj and count the resulting pairs
assuming at most one pair per string.
FIN
my %count;
diff --git a/challenge-256/wlmb/perl/ch-1a.pl b/challenge-256/wlmb/perl/ch-1a.pl
new file mode 100755
index 0000000000..8da7ff0e39
--- /dev/null
+++ b/challenge-256/wlmb/perl/ch-1a.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 256
+# Task 1: Maximum Pairs
+#
+# See https://wlmb.github.io/2024/02/12/PWC256/#task-1-maximum-pairs
+use v5.36;
+use List::Util qw(sum0);
+die <<~"FIN" unless @ARGV;
+ Usage: $0 S1 [S2...]
+ to pair Si with the reverse Sj and count the resulting pairs
+ FIN
+my %count;
+++$count{$_} for (@ARGV);
+my $result = (sum0 map {
+ my $reverse=reverse $_;
+ my $selfreverse=$_ eq $reverse;
+ $count{$_}*(($count{$reverse}//0)-$selfreverse)
+} keys %count)/2;
+say "@ARGV -> $result";