From 0502d0ee7e7c8cab6bc91b96bd1961ae342f64f4 Mon Sep 17 00:00:00 2001 From: James Smith Date: Fri, 17 Mar 2023 14:21:55 +0000 Subject: Update README.md --- challenge-208/james-smith/README.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/challenge-208/james-smith/README.md b/challenge-208/james-smith/README.md index 78914ce0c0..60038de5c3 100644 --- a/challenge-208/james-smith/README.md +++ b/challenge-208/james-smith/README.md @@ -24,13 +24,14 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-208/ja We proceed to do a pass of each array. ```perl sub min_index_sum { - my( $b, %x, $t, $s, @best ) = ( 1e99, - map { $_[0][$_] => $_ } reverse ( 0 .. $#{$_[0]} ) ); #1 - exists $x{$t = $_[1][$_]} && #3 - ( $b > ($s=$x{$t}+$_) ? ($b,@best) = ( $s,$t ) #4 - : $b == $s && push @best, $t ) #5 - for 0 .. $#{$_[1]}; #2 - return \@best; #6 + my( $b, %x, $t, $s, @best ) = ( 1e99, #0 + map { $_[0][$_] => $_ } reverse ( 0 .. $#{$_[0]} ) #1 + ); + exists $x{$t = $_[1][$_]} && #3 + ( $b > ($s=$x{$t}+$_) ? ($b,@best) = ( $s,$t ) #4 + : $b == $s && push @best, $t ) #5 + for 0 .. $#{$_[1]}; #2 + \@best #6 } ``` @@ -40,6 +41,8 @@ We then loop through the second list of strings (`#2`) looking for words which a At the end we just return the current list of words (which could be empty if there are no duplicates). (`#6`) +Note we set the initial best index sum (`#0`) as `10^99` as the index sum will be no where near this and so we can treat this as effectively infinity... + # Task 2: Duplicate and Missing Try all combinations and -- cgit