From 86c920c7e4cfa910783b50e3fcbb85e741a6d19c Mon Sep 17 00:00:00 2001 From: lancew Date: Thu, 28 Mar 2024 22:35:38 +0000 Subject: Initial implementation --- challenge-262/lance-wicks/perl/lib/MPN.pm | 41 +++++++++++++++++++++++++++++++ challenge-262/lance-wicks/perl/t/mpn.t | 38 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 challenge-262/lance-wicks/perl/lib/MPN.pm create mode 100644 challenge-262/lance-wicks/perl/t/mpn.t diff --git a/challenge-262/lance-wicks/perl/lib/MPN.pm b/challenge-262/lance-wicks/perl/lib/MPN.pm new file mode 100644 index 0000000000..9a2190486b --- /dev/null +++ b/challenge-262/lance-wicks/perl/lib/MPN.pm @@ -0,0 +1,41 @@ +package MPN; + + +sub max_pos_or_neg { + my $self = shift; + my @ints = @_; + + my $positives = $self->positives(@ints); + my $negatives = $self->negatives(@ints); + + if ($positives > $negatives) { return $positives } + if ($positives < $negatives) { return $negatives } + return 0; + +} + +sub positives { + my $self = shift; + my @ints = @_; + + my $count = 0; + for my $i (@ints) { + $count++ if $i > 0; + } + + return $count; +} + +sub negatives { + my $self = shift; + my @ints = @_; + + my $count = 0; + for my $i (@ints) { + $count++ if $i < 0; + } + + return $count; +} + +1; \ No newline at end of file diff --git a/challenge-262/lance-wicks/perl/t/mpn.t b/challenge-262/lance-wicks/perl/t/mpn.t new file mode 100644 index 0000000000..4cda02cc02 --- /dev/null +++ b/challenge-262/lance-wicks/perl/t/mpn.t @@ -0,0 +1,38 @@ +use Test2::V0 -target => 'MPN'; + +subtest "Example 1" => sub { + my @ints = (-3, 1, 2, -1, 3, -2, 4); + is $CLASS->positives(@ints), 4; + is $CLASS->negatives(@ints), 3; + is $CLASS->max_pos_or_neg(@ints), 4; +}; + +subtest "Example 2" => sub { + my @ints = (-1, -2, -3, 1); + is $CLASS->positives(@ints), 1; + is $CLASS->negatives(@ints), 3; + is $CLASS->max_pos_or_neg(@ints), 3; +}; + +subtest "Example 3" => sub { + my @ints = (1, 2); + is $CLASS->positives(@ints), 2; + is $CLASS->negatives(@ints), 0; + is $CLASS->max_pos_or_neg(@ints), 2; +}; + +subtest "Example 3" => sub { + my @ints = (1, 2); + is $CLASS->positives(@ints), 2; + is $CLASS->negatives(@ints), 0; + is $CLASS->max_pos_or_neg(@ints), 2; +}; + +subtest "Zero is neither positive or negative" => sub { + my @ints = (-0,+0,0); + is $CLASS->positives(@ints), 0; + is $CLASS->negatives(@ints), 0; + is $CLASS->max_pos_or_neg(@ints), 0; +}; + +done_testing; \ No newline at end of file -- cgit