aboutsummaryrefslogtreecommitdiff
path: root/challenge-252/barroff/perl
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-252/barroff/perl')
-rw-r--r--challenge-252/barroff/perl/ch-1.pl27
-rw-r--r--challenge-252/barroff/perl/ch-2.pl31
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-252/barroff/perl/ch-1.pl b/challenge-252/barroff/perl/ch-1.pl
new file mode 100644
index 0000000000..209c549dbe
--- /dev/null
+++ b/challenge-252/barroff/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+use List::Util qw/sum/;
+
+sub special_numbers (@ints) {
+ sum( map( { $ints[ $_ - 1 ]**2 } grep( { @ints % $_ == 0 } 1 .. @ints ) ) );
+}
+
+sub MAIN() {
+ if (@ARGV) {
+
+ #| Run on command line argument
+ say special_numbers(@ARGV);
+ }
+ else {
+ #| Run test cases
+ use Test2::V0 qw( is plan );
+ plan 2;
+ is special_numbers( ( 1, 2, 3, 4 ) ), 21, 'Works for (1, 2, 3, 4)';
+ is special_numbers( ( 2, 7, 1, 19, 18, 3 ) ), 63,
+ 'Works for (2, 7, 1, 19, 18, 3)';
+ }
+}
+
+MAIN();
diff --git a/challenge-252/barroff/perl/ch-2.pl b/challenge-252/barroff/perl/ch-2.pl
new file mode 100644
index 0000000000..ecc45123ff
--- /dev/null
+++ b/challenge-252/barroff/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+sub unique_sum_zero ($n) {
+ my @ints;
+ map( { push( @ints, $_, 0 - $_ ) } 1 .. int( $n / 2 ) );
+ push( @ints, 0 ) if $n % 2 == 1;
+ @ints = sort( { $a <=> $b } @ints );
+ return \@ints;
+}
+
+sub MAIN() {
+ if (@ARGV) {
+
+ #| Run on command line argument
+ say @{ unique_sum_zero( $ARGV[0] ) };
+ }
+ else {
+ #| Run test cases
+ use Test2::V0 qw( is plan );
+ plan 4;
+
+ is unique_sum_zero(5), [ -2, -1, 0, 1, 2 ], 'works for 5';
+ is unique_sum_zero(3), [ -1, 0, 1 ], 'works for 3';
+ is unique_sum_zero(1), [0], 'works for 1';
+ is unique_sum_zero(0), [], 'works for 0';
+ }
+}
+
+MAIN();