diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2024-02-16 08:36:44 -0600 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2024-02-16 08:36:44 -0600 |
| commit | 3b50c08c0360f94c25df0e78581b0333d07edf4d (patch) | |
| tree | a41898591c32ba1248b1e8e5ee595db638fa4e9c | |
| parent | c9792b8a02f80c49e9bb4f0a1056c98d41e693a4 (diff) | |
| download | perlweeklychallenge-club-3b50c08c0360f94c25df0e78581b0333d07edf4d.tar.gz perlweeklychallenge-club-3b50c08c0360f94c25df0e78581b0333d07edf4d.tar.bz2 perlweeklychallenge-club-3b50c08c0360f94c25df0e78581b0333d07edf4d.zip | |
Add alternative solution
| -rwxr-xr-x | challenge-256/wlmb/perl/ch-1.pl | 2 | ||||
| -rwxr-xr-x | challenge-256/wlmb/perl/ch-1a.pl | 19 |
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"; |
