diff options
| author | wanderdoc <wanderdoc@users.noreply.github.com> | 2025-05-06 19:21:08 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-06 19:21:08 +0200 |
| commit | a3004b66fe54f51311af49e49a071f69035113bd (patch) | |
| tree | 10c7cd44e555c597de708329a0808729fe9bfb00 | |
| parent | 8155b0e7b32c7918210367d40d14e1bd2aba174b (diff) | |
| download | perlweeklychallenge-club-a3004b66fe54f51311af49e49a071f69035113bd.tar.gz perlweeklychallenge-club-a3004b66fe54f51311af49e49a071f69035113bd.tar.bz2 perlweeklychallenge-club-a3004b66fe54f51311af49e49a071f69035113bd.zip | |
Create ch-1.pl
| -rw-r--r-- | challenge-320/wanderdoc/perl/ch-1.pl | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/challenge-320/wanderdoc/perl/ch-1.pl b/challenge-320/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..8312b620fb --- /dev/null +++ b/challenge-320/wanderdoc/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given an array of integers. +Write a script to return the maximum between the number of positive and negative integers. Zero is neither positive nor negative. + +Example 1 + +Input: @ints = (-3, -2, -1, 1, 2, 3) +Output: 3 + +There are 3 positive integers. +There are 3 negative integers. +The maximum between 3 and 3 is 3. + + +Example 2 + +Input: @ints = (-2, -1, 0, 0, 1) +Output: 2 + +There are 1 positive integers. +There are 2 negative integers. +The maximum between 2 and 1 is 2. + + +Example 3 + +Input: @ints = (1, 2, 3, 4) +Output: 4 + +There are 4 positive integers. +There are 0 negative integers. +The maximum between 4 and 0 is 4. +=cut + + + +use List::Util qw(max); +use Test2::V0 -no_srand => 1; + +is(max_count(-3, -2, -1, 1, 2, 3), 3, 'Example 1'); +is(max_count(-2, -1, 0, 0, 1), 2, 'Example 2'); +is(max_count(1, 2, 3, 4), 4, 'Example 3'); +done_testing(); + + +sub max_count +{ + my @arr = @_; + my $pos = my $neg = 0; + for my $elm ( @arr ) + { + if ($elm > 0) {$pos++;} + elsif ($elm < 0 ) {$neg++;} + } + return max($pos, $neg); +} |
