aboutsummaryrefslogtreecommitdiff
path: root/challenge-106/abigail
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-106/abigail')
-rw-r--r--challenge-106/abigail/README.md1
-rw-r--r--challenge-106/abigail/perl/ch-1.pl39
2 files changed, 40 insertions, 0 deletions
diff --git a/challenge-106/abigail/README.md b/challenge-106/abigail/README.md
index 8eb9639369..b5ed6903c3 100644
--- a/challenge-106/abigail/README.md
+++ b/challenge-106/abigail/README.md
@@ -21,6 +21,7 @@ Output: 0
~~~~
### Solutions
+* [Perl](perl/ch-1.pl)
### Blog
[]()
diff --git a/challenge-106/abigail/perl/ch-1.pl b/challenge-106/abigail/perl/ch-1.pl
new file mode 100644
index 0000000000..ad01e97b4c
--- /dev/null
+++ b/challenge-106/abigail/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+use List::Util qw [max];
+
+#
+# See ../README.md
+#
+
+#
+# Run as: perl ch-1.pl < input-file
+#
+
+while (<>) {
+ #
+ # Read numbers, and sort them.
+ #
+ my @N = sort {$a <=> $b} split;
+
+ #
+ # Find the maximum of successive elements, and print them.
+ # Note that the array is sorted, so $N [$_] - $N [$_ - 1]
+ # is always a non-negative number -- no need to take the
+ # absolute value.
+ #
+ # If we have just one number, $#N will be 0, so the map
+ # returns an empty list. In that case, max returns undef.
+ # Hence the // 0.
+ #
+ say max (map {$N [$_] - $N [$_ - 1]} 1 .. $#N) // 0;
+}