aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-208/bob-lied/perl/ch-1.pl16
1 files changed, 10 insertions, 6 deletions
diff --git a/challenge-208/bob-lied/perl/ch-1.pl b/challenge-208/bob-lied/perl/ch-1.pl
index d2b5351331..b62e9e0927 100644
--- a/challenge-208/bob-lied/perl/ch-1.pl
+++ b/challenge-208/bob-lied/perl/ch-1.pl
@@ -61,14 +61,18 @@ say "(", join(",", map { qq("$_") } minIndexSum(\@list1, \@list2)->@*), ")";
# [ a, b ] becomes { a => 0, b => 1 }
sub asHash($list)
{
- my %h = map { $list->[$_] => $_ } 0 .. $#{$list};
+ my %h;
+ # If there are duplicate values in the list, we want to
+ # retain only the first, lesser, index.
+ for my $i ( 0 .. $#{$list} )
+ {
+ $h{$list->[$i]} = $i unless exists $h{$list->[$i]};
+ }
return \%h;
}
sub minIndexSum($list1, $list2)
{
- my @result;
-
my ($h1, $h2) = ( asHash($list1), asHash($list2) );
my %indexSum = map { $_ => ( $h1->{$_} + $h2->{$_} ) }
@@ -76,9 +80,7 @@ sub minIndexSum($list1, $list2)
my $min = min(values %indexSum);
- @result = grep { $indexSum{$_} == $min } keys %indexSum;
-
- return \@result;
+ return [ sort grep { $indexSum{$_} == $min } keys %indexSum ];
}
sub runTest
@@ -96,6 +98,8 @@ sub runTest
is( minIndexSum( [ qw(A B C) ], [ ] ), [], "list 2 empty");
is( minIndexSum( [ ], [ ] ), [], "both lists empty");
+ is( minIndexSum( [ qw(A B C) ], [ qw(C B B) ] ), [ "B","C" ], "Non-unique list");
+
done_testing;
}