diff options
| author | lancew <lw@judocoach.com> | 2024-03-28 22:35:38 +0000 |
|---|---|---|
| committer | lancew <lw@judocoach.com> | 2024-03-28 22:35:38 +0000 |
| commit | 86c920c7e4cfa910783b50e3fcbb85e741a6d19c (patch) | |
| tree | 702edc9a5f0562a58c1da572c2fc66f0bef6cfd0 | |
| parent | 456d249ee58df36bfec89c436f4f7015a84d50dc (diff) | |
| download | perlweeklychallenge-club-86c920c7e4cfa910783b50e3fcbb85e741a6d19c.tar.gz perlweeklychallenge-club-86c920c7e4cfa910783b50e3fcbb85e741a6d19c.tar.bz2 perlweeklychallenge-club-86c920c7e4cfa910783b50e3fcbb85e741a6d19c.zip | |
Initial implementation
| -rw-r--r-- | challenge-262/lance-wicks/perl/lib/MPN.pm | 41 | ||||
| -rw-r--r-- | challenge-262/lance-wicks/perl/t/mpn.t | 38 |
2 files changed, 79 insertions, 0 deletions
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 |
