diff options
| -rw-r--r-- | challenge-245/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-245/wlmb/perl/ch-1.pl | 19 | ||||
| -rwxr-xr-x | challenge-245/wlmb/perl/ch-2.pl | 44 |
3 files changed, 64 insertions, 0 deletions
diff --git a/challenge-245/wlmb/blog.txt b/challenge-245/wlmb/blog.txt new file mode 100644 index 0000000000..0719f68949 --- /dev/null +++ b/challenge-245/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/11/27/PWC245/ diff --git a/challenge-245/wlmb/perl/ch-1.pl b/challenge-245/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..bb318bfa83 --- /dev/null +++ b/challenge-245/wlmb/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +# Perl weekly challenge 245 +# Task 1: Sort Language +# +# See https://wlmb.github.io/2023/11/27/PWC245/#task-1-sort-language +use v5.36; +die <<~"FIN" unless @ARGV && @ARGV%2==0; + Usage: $0 L1 P1 [L2 P2...] + to sort the space separated list of languages Ln + according to their popularity Pn + FIN +while(@ARGV){ + my @language=split " ", shift; + my @popularity=split " ", shift; + warn("Number of elements should coincide: languages: @language vs. popularities @popularity\n"), + next unless @language==@popularity; + my @sorted=map {$language[$_]} sort {$popularity[$a]<=>$popularity[$b]} 0..@language-1; + say "languages: @language\npopularities: @popularity\nsorted: @sorted\n"; +} diff --git a/challenge-245/wlmb/perl/ch-2.pl b/challenge-245/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..2c1b27e2a9 --- /dev/null +++ b/challenge-245/wlmb/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl +# Perl weekly challenge 245 +# Task 2: Largest of Three +# +# See https://wlmb.github.io/2023/11/27/PWC245/#task-2-largest-of-three +use v5.36; +use List::Util qw(all); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to find the largest concatenation of numbers N_i which yield a + multiple of 3. + FIN +die "Only non-negative numbers allowed" unless all {/\d+/} @ARGV; +my $index=0; +my $total; +my @one; +my @two; +my @sorted= sort{$a<=>$b}@ARGV; +for(@sorted){ + my $residue=$_%3; + push @one, $index if $residue==1; + push @two, $index if $residue==2; + $total+=$residue; + ++$index; +} +$total%=3; +my @candidates; +if($total==1){ + push @candidates, [$one[0]] if(@one>=1); + push @candidates, [@two[1,0]] if(@two>=2); +} +if($total==2){ + push @candidates, [@one[1,0]] if(@one>=2); + push @candidates, [$two[0]] if(@two>=1); +} +if(@candidates){ + my @to_remove=map {join "", @sorted[@$_]} @candidates; + my $index_to_remove=@candidates == 2 && $to_remove[1]<$to_remove[0]?1:0; + splice @sorted, $_, 1 for @{$candidates[$index_to_remove]}; +} +my $result=join "", reverse @sorted; +$result=-1 if $result eq ""; +$result=0 if $result==0; # "0000->0"; +say "@ARGV -> $result"; |
