aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-06-23 11:03:47 +0100
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-06-23 11:03:47 +0100
commit16248e9919dba10def1ff1596ca84ad0b460b3f0 (patch)
treee7668893d67484fd0e1fe95f28b6a8140873d268
parent99d8fa43930abb471fac2b94b68c0785619b37fc (diff)
downloadperlweeklychallenge-club-16248e9919dba10def1ff1596ca84ad0b460b3f0.tar.gz
perlweeklychallenge-club-16248e9919dba10def1ff1596ca84ad0b460b3f0.tar.bz2
perlweeklychallenge-club-16248e9919dba10def1ff1596ca84ad0b460b3f0.zip
Week 327 - Missing and mad
-rw-r--r--challenge-327/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-327/peter-campbell-smith/perl/ch-1.pl34
-rwxr-xr-xchallenge-327/peter-campbell-smith/perl/ch-2.pl42
3 files changed, 77 insertions, 0 deletions
diff --git a/challenge-327/peter-campbell-smith/blog.txt b/challenge-327/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..9d37e43d9a
--- /dev/null
+++ b/challenge-327/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/327
diff --git a/challenge-327/peter-campbell-smith/perl/ch-1.pl b/challenge-327/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..6bf57a15b6
--- /dev/null
+++ b/challenge-327/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-06-23
+use utf8; # Week 327 - task 1 - Missing integers
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+missing_integers(1, 2, 1, 3, 2, 5);
+missing_integers(1, 1, 1);
+missing_integers(2, 2, 1);
+missing_integers(7, 8, 9);
+missing_integers(5, 5, 5, 5, 5);
+missing_integers(5, 4, 3, 2, 1);
+
+sub missing_integers {
+
+ my (@ints, @present, @absent);
+
+ # initialise
+ @ints = @_;
+
+ # set $present[$j] to 1 if $j is present in @ints
+ $present[$ints[$_]] = defined $ints[$_] ? 1 : 0 for 0 .. $#ints;
+
+ # push $j onto @absent if $present[$j] is not defined
+ push @absent, grep {not defined $present[$_]} 1 .. scalar @ints;
+
+ # report results
+ say qq[\nInput: (] . join(', ', @ints) . q[)];
+ say qq[Output: (] . join(', ', @absent) . q[)];
+}
diff --git a/challenge-327/peter-campbell-smith/perl/ch-2.pl b/challenge-327/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..c76b53670a
--- /dev/null
+++ b/challenge-327/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-06-23
+use utf8; # Week 327 - task 2 - Mad
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+mad(4, 1, 2, 3);
+mad(1, 3, 7, 11, 15);
+mad(1, 5, 3, 8);
+
+# bigger example
+my (@input);
+push @input, int(rand(10000)) for 0 .. 74;
+mad(@input);
+
+sub mad {
+
+ my(@input, $i, $j, %difference, @sorted);
+
+ # initialise (and sort to match Mohammad's output)
+ @input = sort @_;
+
+ # create all pairs
+ for $i (0 .. $#input - 1) {
+ for $j ($i + 1 .. $#input) {
+
+ # store the differences in $difference{$diff}
+ $difference{abs($input[$i] - $input[$j])} .= qq[[$input[$i], $input[$j]], ];
+ }
+ }
+
+ # sort the keys of @difference - so $sorted[0] is the least
+ @sorted = sort {$a <=> $b} keys %difference;
+
+ # report
+ say qq[\nInput: (] . join(', ', @_) . q[)];
+ say qq[Output: ] . substr($difference{$sorted[0]}, 0, -2);
+}