aboutsummaryrefslogtreecommitdiff
path: root/challenge-252
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2024-01-15 12:35:05 +0100
committerE. Choroba <choroba@matfyz.cz>2024-01-15 12:35:05 +0100
commit04368ad24f28250401393f1096c9f6a323e9ef7a (patch)
tree4d370b98c8553e47ac11705e896bcef648ccfec9 /challenge-252
parent7efb373bb9adffa79f84825217015835805298b5 (diff)
downloadperlweeklychallenge-club-04368ad24f28250401393f1096c9f6a323e9ef7a.tar.gz
perlweeklychallenge-club-04368ad24f28250401393f1096c9f6a323e9ef7a.tar.bz2
perlweeklychallenge-club-04368ad24f28250401393f1096c9f6a323e9ef7a.zip
Solve 252: Special Numbers & Unique Sum Zero by E. Choroba
Diffstat (limited to 'challenge-252')
-rwxr-xr-xchallenge-252/e-choroba/perl/ch-1.pl16
-rwxr-xr-xchallenge-252/e-choroba/perl/ch-2.pl20
2 files changed, 36 insertions, 0 deletions
diff --git a/challenge-252/e-choroba/perl/ch-1.pl b/challenge-252/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..6b5022d0c3
--- /dev/null
+++ b/challenge-252/e-choroba/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use List::Util qw{ sum };
+
+sub special_numbers(@ints) {
+ my @s = @ints[grep 0 == @ints % ($_ + 1), 0 .. $#ints];
+ return sum(map $_ * $_, @s)
+}
+
+use Test::More tests => 2;
+
+is special_numbers(1, 2, 3, 4), 21, 'Example 1';
+is special_numbers(2, 7, 1, 19, 18, 3), 63, 'Example 2';
diff --git a/challenge-252/e-choroba/perl/ch-2.pl b/challenge-252/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..321c4500cc
--- /dev/null
+++ b/challenge-252/e-choroba/perl/ch-2.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub unique_sum_zero($n) {
+ return [(0) x ($n % 2), map { $_, -$_ } 1 .. $n / 2]
+}
+
+use Test::More tests => 7;
+use List::Util qw{ sum uniq };
+
+subtest "n=$_" => sub {
+ plan tests => 3;
+ my $output = unique_sum_zero($_);
+ is scalar @$output, $_, 'length';
+ my @u = uniq(@$output);
+ is scalar @u, $_, 'uniq';
+ is sum(@$output), 0, 'sum 0';
+} for 1 .. 7;