aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Muth <matthias.muth@gmx.de>2025-09-17 23:18:58 +0200
committerMatthias Muth <matthias.muth@gmx.de>2025-09-17 23:18:58 +0200
commitfa4d0c75dc5273373f91dd6b68f26f96742a7d1c (patch)
tree389782fcb783f2c3c99bb6a5a1546201d0c1bc0b
parent08d9b5c480fb82febeb5a31c6fc3c7dbfa69fa69 (diff)
downloadperlweeklychallenge-club-fa4d0c75dc5273373f91dd6b68f26f96742a7d1c.tar.gz
perlweeklychallenge-club-fa4d0c75dc5273373f91dd6b68f26f96742a7d1c.tar.bz2
perlweeklychallenge-club-fa4d0c75dc5273373f91dd6b68f26f96742a7d1c.zip
Challenge 339 Task 1 and 2 solutions in Perl by Matthias Muth - Update
-rw-r--r--challenge-339/matthias-muth/perl/ch-1-test-output.txt111
-rwxr-xr-xchallenge-339/matthias-muth/perl/t/ch-1-full-output.t68
2 files changed, 179 insertions, 0 deletions
diff --git a/challenge-339/matthias-muth/perl/ch-1-test-output.txt b/challenge-339/matthias-muth/perl/ch-1-test-output.txt
new file mode 100644
index 0000000000..c3b1d05bd5
--- /dev/null
+++ b/challenge-339/matthias-muth/perl/ch-1-test-output.txt
@@ -0,0 +1,111 @@
+ok 1 - Test 0: max_diff( 5, 4, 3, 2 ) == 14
+# Pair 1: ( 5, 4 )
+# Pair 2: ( 3, 2 )
+# Product Diff: ( 5 * 4 ) - ( 3 * 2 ) => ( 20 - 6 ) => 14
+#
+ok 2 - Test 1: max_diff( -5, 4, 3, 2 ) == 26
+# Pair 1: ( 3, 2 )
+# Pair 2: ( 4, -5 )
+# Product Diff: ( 3 * 2 ) - ( 4 * -5 ) => ( 6 - (-20) ) => 26
+#
+ok 3 - Test 2: max_diff( 5, -4, 3, 2 ) == 26
+# Pair 1: ( 3, 2 )
+# Pair 2: ( 5, -4 )
+# Product Diff: ( 3 * 2 ) - ( 5 * -4 ) => ( 6 - (-20) ) => 26
+#
+ok 4 - Test 3: max_diff( -5, -4, 3, 2 ) == 14
+# Pair 1: ( -4, -5 )
+# Pair 2: ( 3, 2 )
+# Product Diff: ( -4 * -5 ) - ( 3 * 2 ) => ( 20 - 6 ) => 14
+#
+ok 5 - Test 4: max_diff( 5, 4, -3, 2 ) == 26
+# Pair 1: ( 5, 4 )
+# Pair 2: ( 2, -3 )
+# Product Diff: ( 5 * 4 ) - ( 2 * -3 ) => ( 20 - (-6) ) => 26
+#
+ok 6 - Test 5: max_diff( -5, 4, -3, 2 ) == 14
+# Pair 1: ( 2, -3 )
+# Pair 2: ( 4, -5 )
+# Product Diff: ( 2 * -3 ) - ( 4 * -5 ) => ( (-6) - (-20) ) => 14
+#
+ok 7 - Test 6: max_diff( 5, -4, -3, 2 ) == 14
+# Pair 1: ( 2, -3 )
+# Pair 2: ( 5, -4 )
+# Product Diff: ( 2 * -3 ) - ( 5 * -4 ) => ( (-6) - (-20) ) => 14
+#
+ok 8 - Test 7: max_diff( -5, -4, -3, 2 ) == 26
+# Pair 1: ( -4, -5 )
+# Pair 2: ( 2, -3 )
+# Product Diff: ( -4 * -5 ) - ( 2 * -3 ) => ( 20 - (-6) ) => 26
+#
+ok 9 - Test 8: max_diff( 5, 4, 3, -2 ) == 26
+# Pair 1: ( 5, 4 )
+# Pair 2: ( 3, -2 )
+# Product Diff: ( 5 * 4 ) - ( 3 * -2 ) => ( 20 - (-6) ) => 26
+#
+ok 10 - Test 9: max_diff( -5, 4, 3, -2 ) == 14
+# Pair 1: ( 3, -2 )
+# Pair 2: ( 4, -5 )
+# Product Diff: ( 3 * -2 ) - ( 4 * -5 ) => ( (-6) - (-20) ) => 14
+#
+ok 11 - Test 10: max_diff( 5, -4, 3, -2 ) == 14
+# Pair 1: ( 3, -2 )
+# Pair 2: ( 5, -4 )
+# Product Diff: ( 3 * -2 ) - ( 5 * -4 ) => ( (-6) - (-20) ) => 14
+#
+ok 12 - Test 11: max_diff( -5, -4, 3, -2 ) == 26
+# Pair 1: ( -4, -5 )
+# Pair 2: ( 3, -2 )
+# Product Diff: ( -4 * -5 ) - ( 3 * -2 ) => ( 20 - (-6) ) => 26
+#
+ok 13 - Test 12: max_diff( 5, 4, -3, -2 ) == 14
+# Pair 1: ( 5, 4 )
+# Pair 2: ( -2, -3 )
+# Product Diff: ( 5 * 4 ) - ( -2 * -3 ) => ( 20 - 6 ) => 14
+#
+ok 14 - Test 13: max_diff( -5, 4, -3, -2 ) == 26
+# Pair 1: ( -2, -3 )
+# Pair 2: ( 4, -5 )
+# Product Diff: ( -2 * -3 ) - ( 4 * -5 ) => ( 6 - (-20) ) => 26
+#
+ok 15 - Test 14: max_diff( 5, -4, -3, -2 ) == 26
+# Pair 1: ( -2, -3 )
+# Pair 2: ( 5, -4 )
+# Product Diff: ( -2 * -3 ) - ( 5 * -4 ) => ( 6 - (-20) ) => 26
+#
+ok 16 - Test 15: max_diff( -5, -4, -3, -2 ) == 14
+# Pair 1: ( -4, -5 )
+# Pair 2: ( -2, -3 )
+# Product Diff: ( -4 * -5 ) - ( -2 * -3 ) => ( 20 - 6 ) => 14
+#
+ok 17 - Test 16 (201 numbers): max_diff( -100, -99, -98, -97, -96, -95, -94, -93, -92, -91, -90, -89, -88, -87, -86, -85, -84, -83, -82, -81, -80, -79, -78, -77, -76, -75, -74, -73, -72, -71, -70, -69, -68, -67, -66, -65, -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53, -52, -51, -50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 ) == 19702
+# Pair 1: ( 99, 98 )
+# Pair 2: ( 100, -100 )
+# Product Diff: ( 99 * 98 ) - ( 100 * -100 ) => ( 9702 - (-10000) ) => 19702
+#
+ok 18 - Example 1: max_diff( 5, 9, 3, 4, 6 ) == 42
+# Pair 1: ( 9, 6 )
+# Pair 2: ( 4, 3 )
+# Product Diff: ( 9 * 6 ) - ( 4 * 3 ) => ( 54 - 12 ) => 42
+#
+ok 19 - Example 2: max_diff( 1, -2, 3, -4 ) == 10
+# Pair 1: ( 1, -2 )
+# Pair 2: ( 3, -4 )
+# Product Diff: ( 1 * -2 ) - ( 3 * -4 ) => ( (-2) - (-12) ) => 10
+#
+ok 20 - Example 3: max_diff( -3, -1, -2, -4 ) == 10
+# Pair 1: ( -3, -4 )
+# Pair 2: ( -1, -2 )
+# Product Diff: ( -3 * -4 ) - ( -1 * -2 ) => ( 12 - 2 ) => 10
+#
+ok 21 - Example 4: max_diff( 10, 2, 0, 5, 1 ) == 50
+# Pair 1: ( 10, 5 )
+# Pair 2: ( 1, 0 )
+# Product Diff: ( 10 * 5 ) - ( 1 * 0 ) => ( 50 - 0 ) => 50
+#
+ok 22 - Example 5: max_diff( 7, 8, 9, 10, 10 ) == 44
+# Pair 1: ( 10, 10 )
+# Pair 2: ( 8, 7 )
+# Product Diff: ( 10 * 10 ) - ( 8 * 7 ) => ( 100 - 56 ) => 44
+#
+1..22
diff --git a/challenge-339/matthias-muth/perl/t/ch-1-full-output.t b/challenge-339/matthias-muth/perl/t/ch-1-full-output.t
new file mode 100755
index 0000000000..ea3ce416c7
--- /dev/null
+++ b/challenge-339/matthias-muth/perl/t/ch-1-full-output.t
@@ -0,0 +1,68 @@
+#!/usr/bin/env perl
+#
+# The Weekly Challenge - Perl & Raku
+# (https://theweeklychallenge.org)
+#
+# Challenge 339 Task 1: Max Diff
+#
+# test-ch-1.pl - test script for ch-1.pl.
+#
+
+use v5.36;
+
+use lib qw( . .. );
+require "./ch-1.pl";
+
+use Test2::V0 qw( -no_srand );
+
+my @tests = (
+ [ "Test 0", [ +5, +4, +3, +2 ], 14 ],
+ [ "Test 1", [ -5, +4, +3, +2 ], 26 ],
+ [ "Test 2", [ +5, -4, +3, +2 ], 26 ],
+ [ "Test 3", [ -5, -4, +3, +2 ], 14 ],
+ [ "Test 4", [ +5, +4, -3, +2 ], 26 ],
+ [ "Test 5", [ -5, +4, -3, +2 ], 14 ],
+ [ "Test 6", [ +5, -4, -3, +2 ], 14 ],
+ [ "Test 7", [ -5, -4, -3, +2 ], 26 ],
+ [ "Test 8", [ +5, +4, +3, -2 ], 26 ],
+ [ "Test 9", [ -5, +4, +3, -2 ], 14 ],
+ [ "Test 10", [ +5, -4, +3, -2 ], 14 ],
+ [ "Test 11", [ -5, -4, +3, -2 ], 26 ],
+ [ "Test 12", [ +5, +4, -3, -2 ], 14 ],
+ [ "Test 13", [ -5, +4, -3, -2 ], 26 ],
+ [ "Test 14", [ +5, -4, -3, -2 ], 26 ],
+ [ "Test 15", [ -5, -4, -3, -2 ], 14 ],
+
+ [ "Test 16 (201 numbers)", [ -100..+100 ], 19702 ],
+
+ [ "Example 1", [ 5, 9, 3, 4, 6 ], 42 ],
+ [ "Example 2", [ 1, -2, 3, -4 ], 10 ],
+ [ "Example 3", [ -3, -1, -2, -4 ], 10 ],
+ [ "Example 4", [ 10, 2, 0, 5, 1 ], 50 ],
+ [ "Example 5", [ 7, 8, 9, 10, 10 ], 44 ],
+);
+
+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 "";
+ }
+}
+
+done_testing;