diff options
| author | Matthias Muth <matthias.muth@gmx.de> | 2025-09-17 23:10:18 +0200 |
|---|---|---|
| committer | Matthias Muth <matthias.muth@gmx.de> | 2025-09-17 23:10:18 +0200 |
| commit | 08d9b5c480fb82febeb5a31c6fc3c7dbfa69fa69 (patch) | |
| tree | bc08ff115a01e9a4e09447704d728b8009976252 | |
| parent | 1d2fbb45a66f0494a02a894c2a3bcf5dba5ff643 (diff) | |
| download | perlweeklychallenge-club-08d9b5c480fb82febeb5a31c6fc3c7dbfa69fa69.tar.gz perlweeklychallenge-club-08d9b5c480fb82febeb5a31c6fc3c7dbfa69fa69.tar.bz2 perlweeklychallenge-club-08d9b5c480fb82febeb5a31c6fc3c7dbfa69fa69.zip | |
Challenge 339 Task 1 and 2 solutions in Perl by Matthias Muth - Update
| -rwxr-xr-x | challenge-339/matthias-muth/perl/ch-1.pl | 68 | ||||
| -rwxr-xr-x | challenge-339/matthias-muth/perl/t/ch-1.t | 21 |
2 files changed, 29 insertions, 60 deletions
diff --git a/challenge-339/matthias-muth/perl/ch-1.pl b/challenge-339/matthias-muth/perl/ch-1.pl index 0f94b1e290..8592bc1e8e 100755 --- a/challenge-339/matthias-muth/perl/ch-1.pl +++ b/challenge-339/matthias-muth/perl/ch-1.pl @@ -49,50 +49,34 @@ sub max_diff( @ints ) { # Try the lowest *second* product first, # with the three options for the first product after that. - if ( @negatives ) { - if ( @positives ) { - # We have negatives and positives. - # Use the best possible mixed pair for the second product. - # For the first product, we then have one less positive and - # one less negative available. - my $got_positive_first_product = false; - if ( @positives >= 3 ) { - push @cases, [ "case 3", - [ @positives[1,2] ], - [ $positives[0], $negatives[-1] ], - ]; - $got_positive_first_product = true; - } - if ( @negatives >= 3 ) { - push @cases, [ "case 4", - [ @negatives[-3,-2] ], - [ $positives[0], $negatives[-1] ], - ]; - $got_positive_first_product = true; - } - # In case we couldn't create either of the two cases above, - # we have to use a 'mixed pair' for the first product: - unless ( $got_positive_first_product ) { - push @cases, [ "case 5", - [ $positives[1], $negatives[-2] ], - [ $positives[0], $negatives[-1] ] - ]; - } + if ( @negatives && @positives ) { + # We have negatives and positives. + # Use the best possible mixed pair for the second product. + # For the first product, we then have one less positive and + # one less negative available. + my $got_positive_first_product = false; + if ( @positives >= 3 ) { + push @cases, [ "case 3", + [ @positives[1,2] ], + [ $positives[0], $negatives[-1] ], + ]; + $got_positive_first_product = true; } - else { - # Only negatives, no positives. - push @cases, [ "case 6", - [ @negatives[-2,-1] ], - [ @negatives[0,1] ] + if ( @negatives >= 3 ) { + push @cases, [ "case 4", + [ @negatives[-3,-2] ], + [ $positives[0], $negatives[-1] ], + ]; + $got_positive_first_product = true; + } + # In case we couldn't create either of the two cases above, + # we have to use a 'mixed pair' for the first product: + unless ( $got_positive_first_product ) { + push @cases, [ "case 5", + [ $positives[1], $negatives[-2] ], + [ $positives[0], $negatives[-1] ] ]; } - } - else { - # Only positives, no negatives. - push @cases, [ "case 7", - [ @positives[0,1] ], - [ @positives[-2,-1] ], - ]; } my $maximum = @@ -116,6 +100,8 @@ sub max_diff( @ints ) { : $maximum; } +# Allow for including this script for the function definitioni, but without +# executing the tests: unless ( caller ) { use Test2::V0 qw( -no_srand ); diff --git a/challenge-339/matthias-muth/perl/t/ch-1.t b/challenge-339/matthias-muth/perl/t/ch-1.t index e1c5b3d32e..15b76466cc 100755 --- a/challenge-339/matthias-muth/perl/t/ch-1.t +++ b/challenge-339/matthias-muth/perl/t/ch-1.t @@ -5,11 +5,10 @@ # # Challenge 339 Task 1: Max Diff # -# test-ch-1.pl - test script for ch-1.pl. +# t/ch-1.t - test script for ch-1.pl. # use v5.36; -use builtin qw( true false ); use lib qw( . .. ); require "./ch-1.pl"; @@ -47,23 +46,7 @@ for ( @tests ) { my ( $test_name, $input, $expected ) = $_->@*; my $descr = "$test_name:" . " max_diff( " . join( ", ", $input->@* ) . " ) == $expected"; - my ( $maximum, $pair_1, $pair_2 ) = max_diff( $input->@* ); - is $maximum, $expected, $descr - and $pair_1 && $pair_2 && do { - # Explain the correct solution. - my $product_1 = $pair_1->[0] * $pair_1->[1]; - my $product_2 = $pair_2->[0] * $pair_2->[1]; - my $result = $product_1 - $product_2; - note " Pair 1: ( $pair_1->[0], $pair_1->[1] )"; - note " Pair 2: ( $pair_2->[0], $pair_2->[1] )"; - note " Product Diff: ", - "( $pair_1->[0] * $pair_1->[1] )", - " - ( $pair_2->[0] * $pair_2->[1] )", - " => ( ", $product_1 >= 0 ? $product_1 : "($product_1)", - " - ", $product_2 >= 0 ? $product_2 : "($product_2)", " )", - " => $result"; - note ""; - } + is max_diff( $input->@* ), $expected, $descr; } done_testing; |
