aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-07 09:19:11 +0100
committerGitHub <noreply@github.com>2020-10-07 09:19:11 +0100
commit6d9c462f619cd7bd69ae1388cc584e184b19eafb (patch)
tree8e7a8ce3891943c4eff7a3129a90dc3f2b119d2b
parent918d6969308d2e464b4db78960dda5b55dbd1d42 (diff)
parent2749223888415b34b9a08611c41255d92192e131 (diff)
downloadperlweeklychallenge-club-6d9c462f619cd7bd69ae1388cc584e184b19eafb.tar.gz
perlweeklychallenge-club-6d9c462f619cd7bd69ae1388cc584e184b19eafb.tar.bz2
perlweeklychallenge-club-6d9c462f619cd7bd69ae1388cc584e184b19eafb.zip
Merge pull request #2466 from andinus/master
Fix bug on challenge-080 ch-1, compact challenge-081 ch-2
-rw-r--r--challenge-080/andinus/README10
-rwxr-xr-xchallenge-080/andinus/perl/ch-1.pl5
-rwxr-xr-xchallenge-081/andinus/perl/ch-2.pl1
3 files changed, 10 insertions, 6 deletions
diff --git a/challenge-080/andinus/README b/challenge-080/andinus/README
index ac701a9412..c81b89495c 100644
--- a/challenge-080/andinus/README
+++ b/challenge-080/andinus/README
@@ -28,8 +28,6 @@ Table of Contents
1.1 Perl
────────
- • *Bug*: This will fail if inputs are repeated.
-
• Initial version didn't check for `1', I might have assumed that it
was accounted for in this line `print "1\n" and exit 0 if
$sorted[$#sorted] < 1;'.
@@ -40,9 +38,13 @@ Table of Contents
• Program: <file:perl/ch-1.pl>
We take input from `@ARGV', sort it & remove all inputs less than 1.
- We check if the smallest positive number is 1.
+ We check if the smallest positive number is 1. We filter repeated
+ inputs using `%hash'.
┌────
- │ my @sorted = sort { $a <=> $b } @ARGV;
+ │ my %hash;
+ │ %hash = map { $_ => 1 } @ARGV;
+ │
+ │ my @sorted = sort { $a <=> $b } keys %hash;
│ # Print 1 if there are no positive numbers in @sorted.
│ print "1\n" and exit 0 if $sorted[$#sorted] < 1;
diff --git a/challenge-080/andinus/perl/ch-1.pl b/challenge-080/andinus/perl/ch-1.pl
index 198be3d999..81121cc425 100755
--- a/challenge-080/andinus/perl/ch-1.pl
+++ b/challenge-080/andinus/perl/ch-1.pl
@@ -6,7 +6,10 @@ use warnings;
die "usage: ./ch-1.pl [space seperated numbers]\n"
unless scalar @ARGV;
-my @sorted = sort { $a <=> $b } @ARGV;
+my %hash;
+%hash = map { $_ => 1 } @ARGV;
+
+my @sorted = sort { $a <=> $b } keys %hash;
# Print 1 if there are no positive numbers in @sorted.
print "1\n" and exit 0
diff --git a/challenge-081/andinus/perl/ch-2.pl b/challenge-081/andinus/perl/ch-2.pl
index 0f17acb868..3dba696bbc 100755
--- a/challenge-081/andinus/perl/ch-2.pl
+++ b/challenge-081/andinus/perl/ch-2.pl
@@ -17,7 +17,6 @@ $file =~ s/\n/ /g;
my %words;
foreach my $word (split / /, $file) {
- $words{$word} = 1 and next unless exists $words{$word};
$words{$word}++;
}