aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2022-11-20 21:48:37 -0500
committerAdam Russell <ac.russell@live.com>2022-11-20 21:48:37 -0500
commitdbc68575d4cd80d8afec86a0ee994a2334f9d588 (patch)
tree94da9a164c1709216824b9e7ab512678083a05de
parentb5791fac00294ed02e86dd60f70cd0416f66ebfd (diff)
downloadperlweeklychallenge-club-dbc68575d4cd80d8afec86a0ee994a2334f9d588.tar.gz
perlweeklychallenge-club-dbc68575d4cd80d8afec86a0ee994a2334f9d588.tar.bz2
perlweeklychallenge-club-dbc68575d4cd80d8afec86a0ee994a2334f9d588.zip
updated Perl solution for Part 1 to be more efficient
-rw-r--r--challenge-191/adam-russell/perl/ch-1.pl18
1 files changed, 6 insertions, 12 deletions
diff --git a/challenge-191/adam-russell/perl/ch-1.pl b/challenge-191/adam-russell/perl/ch-1.pl
index aa6095c94b..f8ac3ec3b6 100644
--- a/challenge-191/adam-russell/perl/ch-1.pl
+++ b/challenge-191/adam-russell/perl/ch-1.pl
@@ -5,21 +5,15 @@ use warnings;
# You are given list of integers, @list. Write a script to find out whether the largest
# item in the list is at least twice as large as each of the other items.
##
-use boolean;
-
sub twice_largest{
my(@list_integers) = @_;
- my $twice_max = -1;
- for my $i (0 .. @list_integers - 1){
- my $twice_rest = true;
- for my $j (0 .. @list_integers - 1){
- unless($i == $j){
- $twice_rest = $list_integers[$i] >= 2 * $list_integers[$j];
- }
+ my @sorted_integers = sort {$a <=> $b} @list_integers;
+ for my $i (@sorted_integers[0 .. @sorted_integers - 1]){
+ unless($sorted_integers[@sorted_integers - 1] == $i){
+ return -1 unless $sorted_integers[@sorted_integers - 1] >= 2 * $i;
}
- $twice_max = $list_integers[$i] if $twice_rest;
- }
- return $twice_max>-1?1:-1;
+ }
+ return 1;
}
MAIN:{