diff options
| -rw-r--r-- | challenge-080/andinus/README | 10 | ||||
| -rwxr-xr-x | challenge-080/andinus/perl/ch-1.pl | 5 | ||||
| -rwxr-xr-x | challenge-081/andinus/perl/ch-2.pl | 1 |
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}++; } |
