aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-25 00:00:17 +0100
committerGitHub <noreply@github.com>2025-06-25 00:00:17 +0100
commit3b709faf56a078c78e2d6dd63b8b74716fb6cb30 (patch)
treee1679eb68ddf7ce970143469922e607ecc22c51c
parente1863f66b8e0f2b16ef61d553baabdbe38f56a86 (diff)
parent8353403f022bdd318b590d2fcf56210c04917cfa (diff)
downloadperlweeklychallenge-club-3b709faf56a078c78e2d6dd63b8b74716fb6cb30.tar.gz
perlweeklychallenge-club-3b709faf56a078c78e2d6dd63b8b74716fb6cb30.tar.bz2
perlweeklychallenge-club-3b709faf56a078c78e2d6dd63b8b74716fb6cb30.zip
Merge pull request #12234 from spuelrich/spuelrich-ch327
challenge 327
-rw-r--r--challenge-327/spuelrich/perl/README1
-rwxr-xr-xchallenge-327/spuelrich/perl/ch-1.pl21
-rwxr-xr-xchallenge-327/spuelrich/perl/ch-2.pl41
3 files changed, 63 insertions, 0 deletions
diff --git a/challenge-327/spuelrich/perl/README b/challenge-327/spuelrich/perl/README
new file mode 100644
index 0000000000..dbdeadaa09
--- /dev/null
+++ b/challenge-327/spuelrich/perl/README
@@ -0,0 +1 @@
+Solution by spuelrich \ No newline at end of file
diff --git a/challenge-327/spuelrich/perl/ch-1.pl b/challenge-327/spuelrich/perl/ch-1.pl
new file mode 100755
index 0000000000..bc6ff211a7
--- /dev/null
+++ b/challenge-327/spuelrich/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use v5.40;
+
+sub ch_327_1 {
+ my @ints = @_;
+
+ my %all = map {($_,1)} 1..@ints;
+ delete @all{@ints};
+ my @missing = sort keys %all;
+
+ local $" = ', ';
+ say "Inputs: (@ints)";
+ say "Outputs: (@missing)";
+}
+
+ch_327_1(1, 2, 1, 3, 2, 5);
+ch_327_1(1, 1, 1);
+ch_327_1(2, 2, 1);
diff --git a/challenge-327/spuelrich/perl/ch-2.pl b/challenge-327/spuelrich/perl/ch-2.pl
new file mode 100755
index 0000000000..54170e0b24
--- /dev/null
+++ b/challenge-327/spuelrich/perl/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use v5.40;
+
+sub ch_327_2 {
+ my @ints = @_;
+
+ local $" = ', ';
+ say "Inputs: (@ints)";
+
+ my @all_pairs;
+ while (@ints >=2) {
+ my $first = shift @ints;
+ push @all_pairs, map {[ $first, $_ ]} @ints;
+ }
+
+ my $mad;
+ for my $p (@all_pairs) {
+ my $diff = abs($p->[0] - $p->[1]);
+ $mad //= $diff;
+ if ($diff < $mad) {
+ $mad = $diff;
+ }
+ }
+
+ say "MAD: $mad";
+ say 'Output: ' . join(', ', (
+ map {"[@$_]"}
+ grep {abs($_->[0] - $_->[1]) == $mad}
+ @all_pairs
+ ));
+ say q();
+}
+
+ch_327_2(4, 1, 2, 3);
+ch_327_2(1, 3, 7, 11, 15);
+ch_327_2(1, 5, 3, 8);
+ch_327_2(1, 8, 17, 24);
+