diff options
| author | Abigail <abigail@abigail.be> | 2021-03-29 12:51:05 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-29 12:51:05 +0200 |
| commit | 3b499d0bcb9e20fd039ca88adf1b0ef22158233a (patch) | |
| tree | df78469fb610d5ca89d07663f9b5e1f3c7d44794 /challenge-106/abigail | |
| parent | d141977842ee0a6744e2f8b4b6db81ff1befe549 (diff) | |
| download | perlweeklychallenge-club-3b499d0bcb9e20fd039ca88adf1b0ef22158233a.tar.gz perlweeklychallenge-club-3b499d0bcb9e20fd039ca88adf1b0ef22158233a.tar.bz2 perlweeklychallenge-club-3b499d0bcb9e20fd039ca88adf1b0ef22158233a.zip | |
Perl solution for week 105, part 1
Diffstat (limited to 'challenge-106/abigail')
| -rw-r--r-- | challenge-106/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-106/abigail/perl/ch-1.pl | 39 |
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; +} |
