From dbc68575d4cd80d8afec86a0ee994a2334f9d588 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 20 Nov 2022 21:48:37 -0500 Subject: updated Perl solution for Part 1 to be more efficient --- challenge-191/adam-russell/perl/ch-1.pl | 18 ++++++------------ 1 file 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:{ -- cgit