diff options
| author | pme <hauptadler@gmail.com> | 2024-03-25 13:37:02 +0100 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2024-03-25 13:37:02 +0100 |
| commit | 0e092ff4ce5b51573c68a3eb7a7b967152993ddd (patch) | |
| tree | 25f30d124969719fe53aa8d140c7798967f72ce6 /challenge-262 | |
| parent | 041fe9129e3ef4d86df461a0feeee1b3740d5758 (diff) | |
| download | perlweeklychallenge-club-0e092ff4ce5b51573c68a3eb7a7b967152993ddd.tar.gz perlweeklychallenge-club-0e092ff4ce5b51573c68a3eb7a7b967152993ddd.tar.bz2 perlweeklychallenge-club-0e092ff4ce5b51573c68a3eb7a7b967152993ddd.zip | |
challenge-262
Diffstat (limited to 'challenge-262')
| -rwxr-xr-x | challenge-262/peter-meszaros/perl/ch-1.pl | 66 | ||||
| -rwxr-xr-x | challenge-262/peter-meszaros/perl/ch-2.pl | 62 |
2 files changed, 128 insertions, 0 deletions
diff --git a/challenge-262/peter-meszaros/perl/ch-1.pl b/challenge-262/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..a4a3be4d46 --- /dev/null +++ b/challenge-262/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# +# You are given an array of integers, @ints. +# +# Write a script to return the maximum number of either positive or negative +# integers in the given array. +# Example 1 +# +# Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +# Output: 4 +# +# Count of positive integers: 4 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 4 +# +# Example 2 +# +# Input: @ints = (-1, -2, -3, 1) +# Output: 3 +# +# Count of positive integers: 1 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 3 +# +# Example 3 +# Input: @ints = (1,2) +# Output: 2 +# +# Count of positive integers: 2 +# Count of negative integers: 0 +# Maximum of count of positive and negative integers: 2 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [-3, 1, 2, -1, 3, -2, 4], + [-1, -2, -3, 1], + [1,2], +]; + +sub max_positive_negative +{ + my $l = shift; + + my $pos = 0; + my $neg = 0; + for my $i (@$l) { + if ($i > 0) { + ++$pos; + } elsif ($i < 0) { + ++$neg; + } + } + return $pos > $neg ? $pos : $neg; +} + +is(max_positive_negative($cases->[0]), 4, 'Example 1'); +is(max_positive_negative($cases->[1]), 3, 'Example 2'); +is(max_positive_negative($cases->[2]), 2, 'Example 3'); +done_testing(); + +exit 0; diff --git a/challenge-262/peter-meszaros/perl/ch-2.pl b/challenge-262/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..36e108b69f --- /dev/null +++ b/challenge-262/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# +# You are given an array of integers, @ints and an integer $k. +# +# Write a script to return the number of pairs (i, j) where +# +# a) 0 <= i < j < size of @ints +# b) ints[i] == ints[j] +# c) i x j is divisible by k +# +# Example 1 +# +# Input: @ints = (3,1,2,2,2,1,3) and $k = 2 +# Output: 4 +# +# (0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2 +# (2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2 +# (2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2 +# (3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2 +# +# Example 2 +# +# Input: @ints = (1,2,3) and $k = 1 +# Output: 0 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[3, 1, 2, 2, 2, 1, 3], 2], + [[1, 2, 3], 1], +]; + +sub count_equal_divisable +{ + my $l = $_[0]->[0]; + my $k = $_[0]->[1]; + + my $r = 0; + my $c = 0; + while (my $i = shift @$l) { + for my $n (0..$#$l) { + if ($i == $l->[$n]) { + my $v = $c * $n / $k; + ++$r if $v == int($v); + } + } + ++$c; + } + + return $r; +} + +is(count_equal_divisable($cases->[0]), 4, 'Example 1'); +is(count_equal_divisable($cases->[1]), 0, 'Example 2'); +done_testing(); + +exit 0; + |
