aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-07 17:27:16 +0100
committerGitHub <noreply@github.com>2024-05-07 17:27:16 +0100
commit655a40c2a6572a2d88b93a2c450059b75ab57adc (patch)
tree5d9188ef9e3b4c71faf7d0eb63538d1c03498909
parent78dd0a4ccc0ef62338b6d44812ae259a2608e65d (diff)
parenta22199d66d2245a0b39a11580ed18ade2ca418ad (diff)
downloadperlweeklychallenge-club-655a40c2a6572a2d88b93a2c450059b75ab57adc.tar.gz
perlweeklychallenge-club-655a40c2a6572a2d88b93a2c450059b75ab57adc.tar.bz2
perlweeklychallenge-club-655a40c2a6572a2d88b93a2c450059b75ab57adc.zip
Merge pull request #10061 from boblied/w268
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;
}