aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Lied <boblied+github@gmail.com>2024-05-07 05:47:44 -0500
committerBob Lied <boblied+github@gmail.com>2024-05-07 05:47:44 -0500
commita22199d66d2245a0b39a11580ed18ade2ca418ad (patch)
tree5d86b0f8eea411a4cee89ce14623452db2ffad4a
parentb2634305dc8568620aa4080c6d03decbc7c781b2 (diff)
downloadperlweeklychallenge-club-a22199d66d2245a0b39a11580ed18ade2ca418ad.tar.gz
perlweeklychallenge-club-a22199d66d2245a0b39a11580ed18ade2ca418ad.tar.bz2
perlweeklychallenge-club-a22199d66d2245a0b39a11580ed18ade2ca418ad.zip
Add validatation for task 1
-rw-r--r--challenge-268/bob-lied/perl/ch-1.pl25
1 files changed, 24 insertions, 1 deletions
diff --git a/challenge-268/bob-lied/perl/ch-1.pl b/challenge-268/bob-lied/perl/ch-1.pl
index e4512bc29d..faaffefebc 100644
--- a/challenge-268/bob-lied/perl/ch-1.pl
+++ b/challenge-268/bob-lied/perl/ch-1.pl
@@ -28,13 +28,32 @@ use Getopt::Long;
my $Verbose = 0;
my $DoTest = 0;
+sub mn :prototype(\@@)
+{
+ my @x = @{shift @_};
+ my @y = @_;
+ say "x: (", join(",", @x), ")";
+ say "y: (", join(",", @y), ")";
+}
+my @x=(1,2); my @y=(3,4);
+mn(@x, @y);
+
GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
exit(!runTest()) if $DoTest;
+sub validate($x, $y, $magic)
+{
+ return undef if $x->$#* != $y->$#*;
+ my %should_be_y;
+ $should_be_y{$_ + $magic} = $_ for $x->@*;
+ delete $should_be_y{$_} for $y->@*;
+ return scalar(%should_be_y) == 0 ? $magic : undef;
+}
+
sub magicNumber($x, $y)
{
use List::Util qw/min/;
- return min($y->@*) - min($x->@*);
+ return validate($x, $y, min($y->@*) - min($x->@*));
}
sub runTest
@@ -45,5 +64,9 @@ sub runTest
is( magicNumber([1,2,1], [5,4,4]), 3, "Example 2");
is( magicNumber([2 ], [5 ]), 3, "Example 3");
+ is( magicNumber([9 ], [8,7 ]), undef, "Different lengths");
+ is( magicNumber([9,6,4], [7,3,6]), undef, "No magic");
+ is( magicNumber([2,2,5], [4,4,7]), 2, "With repeats");
+
done_testing;
}