The weekly challenge 262: Task 1: Max Positive Negative

 1 #!/usr/bin/env perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-262/#TASK1
 3 #
 4 # Task 1: Max Positive Negative
 5 # =============================
 6 #
 7 # You are given an array of integers, @ints.
 8 #
 9 # Write a script to return the maximum number of either positive or negative
10 # integers in the given array.
11 #
12 ## Example 1
13 ##
14 ## Input: @ints = (-3, 1, 2, -1, 3, -2, 4)
15 ## Output: 4
16 ##
17 ## Count of positive integers: 4
18 ## Count of negative integers: 3
19 ## Maximum of count of positive and negative integers: 4
20 #
21 ## Example 2
22 ##
23 ## Input: @ints = (-1, -2, -3, 1)
24 ## Output: 3
25 ##
26 ## Count of positive integers: 1
27 ## Count of negative integers: 3
28 ## Maximum of count of positive and negative integers: 3
29 #
30 ## Example 3
31 ##
32 ## Input: @ints = (1,2)
33 ## Output: 2
34 ##
35 ## Count of positive integers: 2
36 ## Count of negative integers: 0
37 ## Maximum of count of positive and negative integers: 2
38 #
39 ############################################################
40 ##
41 ## discussion
42 ##
43 ############################################################
44 #
45 # Simply count the negatives and the positives, then return
46 # the bigger of the two numbers.
47 
48 use strict;
49 use warnings;
50 
51 max_positive_negative(-3, 1, 2, -1, 3, -2, 4);
52 max_positive_negative(-1, -2, -3, 1);
53 max_positive_negative(1,2);
54 
55 sub max_positive_negative {
56    my @ints = @_;
57    print "Input: (" . join(", ", @ints) . ")\n";
58    my $pos = 0;
59    my $neg = 0;
60    foreach my $num (@ints) {
61       if($num < 0) {
62          $neg++;
63       } else {
64          $pos++;
65       }
66    }
67    my $result = ($pos > $neg) ? $pos : $neg;
68    print "Output: $result\n";
69 }
70