aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Mahnke <andreas.mahnke@leuphana.de>2025-06-23 16:08:19 +0200
committerAndreas Mahnke <andreas.mahnke@leuphana.de>2025-06-23 16:08:19 +0200
commit2b87511984eeae8afe468261af956aa95fa731c2 (patch)
treeafe0cb366a9eff93fbf5004b49bf9896f9386f15
parent99d8fa43930abb471fac2b94b68c0785619b37fc (diff)
downloadperlweeklychallenge-club-2b87511984eeae8afe468261af956aa95fa731c2.tar.gz
perlweeklychallenge-club-2b87511984eeae8afe468261af956aa95fa731c2.tar.bz2
perlweeklychallenge-club-2b87511984eeae8afe468261af956aa95fa731c2.zip
Challenge 327
-rw-r--r--challenge-327/mahnkong/perl/ch-1.pl18
-rw-r--r--challenge-327/mahnkong/perl/ch-2.pl28
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-327/mahnkong/perl/ch-1.pl b/challenge-327/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..6ca0f89aa4
--- /dev/null
+++ b/challenge-327/mahnkong/perl/ch-1.pl
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@ints) {
+ my @result;
+ my %ints = map { $_ => 1 } @ints;
+
+ for (my $i = 1; $i <= scalar(@ints); $i++,) {
+ push @result, $i unless exists $ints{$i};
+ }
+ return [ @result ];
+}
+
+is_deeply(run(1, 2, 1, 3, 2, 5), [4, 6], "Example 1");
+is_deeply(run(1, 1, 1), [2, 3], "Example 2");
+is_deeply(run(2, 2, 1), [3], "Example 3");
diff --git a/challenge-327/mahnkong/perl/ch-2.pl b/challenge-327/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..60772653cc
--- /dev/null
+++ b/challenge-327/mahnkong/perl/ch-2.pl
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@ints) {
+ my %pairs;
+
+ for (my $i = 0; $i < scalar(@ints); $i++) {
+ for (my $j = 0; $j < scalar(@ints); $j++) {
+ next if $i == $j;
+ $pairs{abs($ints[$i] - $ints[$j])}->{join(":", sort { $a <=> $b } ($ints[$i], $ints[$j]))} = undef;
+ }
+ }
+
+
+ foreach my $key (sort { $a <=> $b } keys(%pairs)) {
+ my @result;
+ foreach my $pair (sort(keys(%{$pairs{$key}}))) {
+ push @result, [ split(':', $pair) ];
+ }
+ return [ @result ];
+ }
+}
+
+is_deeply(run(4, 1, 2, 3), [[1, 2], [2, 3], [3, 4]], "Example 1");
+is_deeply(run(1, 3, 7, 11, 15), [[1, 3]], "Example 2");
+is_deeply(run(1, 5, 3, 8), [[1, 3], [3, 5]], "Example 3");