diff options
| -rw-r--r-- | challenge-262/dave-jacoby/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-262/dave-jacoby/perl/ch-2.pl | 34 |
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-262/dave-jacoby/perl/ch-1.pl b/challenge-262/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..2caa4c1b3c --- /dev/null +++ b/challenge-262/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ -3, 1, 2, -1, 3, -2, 4 ], + [ -1, -2, -3, 1 ], + [ 1, 2 ], + +); + +for my $example (@examples) { + my $output = max_pos_neg( $example->@* ); + my @ints = $example->@*; + my $ints = join ',', @ints; + say <<"END"; + Input: \@ints = ($ints) + Output: $output +END +} + +sub max_pos_neg (@ints) { + my $pos = scalar grep { $_ > 0 } @ints; + my $neg = scalar grep { $_ < 0 } @ints; + return $pos > $neg ? $pos : $neg; +} diff --git a/challenge-262/dave-jacoby/perl/ch-2.pl b/challenge-262/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..3be159219a --- /dev/null +++ b/challenge-262/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + { ints => [ 3, 1, 2, 2, 2, 1, 3 ], k => 2 }, + { ints => [ 1, 2, 3 ], k => 1 }, +); + +for my $example (@examples) { + my $k = $example->{k}; + my @ints = $example->{ints}->@*; + my $output = count_equal_divisible( $k, @ints ); + my $ints = join ',', @ints; + say <<"END"; + Input: \@ints = ($ints) and \$k = $k + Output: $output +END +} + +sub count_equal_divisible ( $k, @ints ) { + my $output = 0; + for my $i ( 0 .. $#ints ) { + for my $j ( $i + 1 .. $#ints ) { + next unless $ints[$i] == $ints[$j]; + next unless ( $i * $j ) % $k == 0; + $output++; + } + } + return $output; +} |
