aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-03 22:51:32 +0000
committerGitHub <noreply@github.com>2023-12-03 22:51:32 +0000
commit269857baaa2d3676bf4b6e5710f34084738556a8 (patch)
treedc086eb7febabe0b6ed4ebbd4295507f73fa2b0d
parent9653a16b1ac2b106fedb93094dd4d044102f34ad (diff)
parent725bd5ff4373b83bc040e582f000773a33b92d78 (diff)
downloadperlweeklychallenge-club-269857baaa2d3676bf4b6e5710f34084738556a8.tar.gz
perlweeklychallenge-club-269857baaa2d3676bf4b6e5710f34084738556a8.tar.bz2
perlweeklychallenge-club-269857baaa2d3676bf4b6e5710f34084738556a8.zip
Merge pull request #9182 from wlmb/challenges
Fix error in task 2
-rwxr-xr-xchallenge-245/wlmb/perl/ch-2.pl20
1 files changed, 12 insertions, 8 deletions
diff --git a/challenge-245/wlmb/perl/ch-2.pl b/challenge-245/wlmb/perl/ch-2.pl
index 2c1b27e2a9..8ee193d592 100755
--- a/challenge-245/wlmb/perl/ch-2.pl
+++ b/challenge-245/wlmb/perl/ch-2.pl
@@ -15,7 +15,7 @@ my $index=0;
my $total;
my @one;
my @two;
-my @sorted= sort{$a<=>$b}@ARGV;
+my @sorted= sort{"$a$b"<=>"$b$a"}@ARGV; # increasing order
for(@sorted){
my $residue=$_%3;
push @one, $index if $residue==1;
@@ -24,21 +24,25 @@ for(@sorted){
++$index;
}
$total%=3;
-my @candidates;
+my @candidates; # if neccesary to remove numbers
if($total==1){
- push @candidates, [$one[0]] if(@one>=1);
+ # remove the smallest number that leaves a residue one
+ push @candidates, [$one[0]] if(@one);
+ # remove the two smallest numbers that leaves a residue two
push @candidates, [@two[1,0]] if(@two>=2);
}
if($total==2){
+ # remove the two smallest numbers that leaves a residue one
push @candidates, [@one[1,0]] if(@one>=2);
- push @candidates, [$two[0]] if(@two>=1);
+ # or the smallest numbers that leaves a residue two
+ push @candidates, [$two[0]] if(@two);
}
-if(@candidates){
+if(@candidates){ # find smallest 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]};
+ splice @sorted, $_, 1 for @{$candidates[$index_to_remove]}; # remove one or two numbers
}
my $result=join "", reverse @sorted;
-$result=-1 if $result eq "";
-$result=0 if $result==0; # "0000->0";
+$result=-1 if $result eq ""; # removed all
+$result=0 if $result==0; # 0000...=0 is divisible;
say "@ARGV -> $result";