From f3941f09b5ed2cbc2da0eef3ea57863eac2c5839 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Tue, 1 Oct 2024 16:21:37 -0400 Subject: DAJ 289 --- challenge-289/dave-jacoby/blog.txt | 1 + challenge-289/dave-jacoby/perl/ch-1.pl | 31 ++++++++++++++++++++++++++ challenge-289/dave-jacoby/perl/ch-2.pl | 40 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 challenge-289/dave-jacoby/blog.txt create mode 100644 challenge-289/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-289/dave-jacoby/perl/ch-2.pl diff --git a/challenge-289/dave-jacoby/blog.txt b/challenge-289/dave-jacoby/blog.txt new file mode 100644 index 0000000000..72e5b2400b --- /dev/null +++ b/challenge-289/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2024/10/01/irregular-use-of-regular-expressions-weekly-challenge-289.html diff --git a/challenge-289/dave-jacoby/perl/ch-1.pl b/challenge-289/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..2220b86022 --- /dev/null +++ b/challenge-289/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +use List::Util qw{ uniq }; + +my @examples = ( + + [ 5, 6, 4, 1 ], + [ 4, 5 ], + [ 1, 2, 2, 3 ], + +); + +for my $example (@examples) { + my $output = third_maximum( $example->@* ); + my $input = join ', ', $example->@*; + say <<"END"; + Input: \$ints = ($input) + Output: $output +END +} + +sub third_maximum (@array) { + my @new = reverse uniq sort @array; + return $new[2] if defined $new[2]; + return shift @new; +} + diff --git a/challenge-289/dave-jacoby/perl/ch-2.pl b/challenge-289/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..a0cf0f97ed --- /dev/null +++ b/challenge-289/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +use List::Util qw{ min max }; + +my @examples = ( + + 'Perl is different. In a nutshell, Perl is designed to make +the easy jobs easy, without making the hard jobs impossible.', + + 'According to Larry Wall(1), the original author of the Perl +programming language, there are three great virtues of a +programmer; Laziness, Impatience and Hubris.', + + q{If you haven't used regular expressions before, a tutorial +introduction is available in perlretut. If you know just a +little about them, a quick-start introduction is available +in perlrequick.}, + + 'THIS looks like a job for RECURSION!', +); + +for my $input (@examples) { + my $output = $input; + $output =~ s{(\w)(\w+)(\w)}{ $1 . jumble_letters( $2 ) . $3 }gmxe; + + say $input; + say '-----'; + say $output; + say ''; +} + +sub jumble_letters ($str) { + $str = join '', sort { rand 10 <=> rand 10 } + split //, $str; + return $str; +} -- cgit