Skip to content

Robbie Hatley's solutions, in Perl, for The Weekly Challenge #309. #11587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions challenge-309/robbie-hatley/blog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://hatley-software.blogspot.com/2025/02/robbie-hatleys-solutions-in-perl-for_16.html
93 changes: 93 additions & 0 deletions challenge-309/robbie-hatley/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env perl

=pod

--------------------------------------------------------------------------------------------------------------
TITLE AND ATTRIBUTION:
Solutions in Perl for The Weekly Challenge 309-1,
written by Robbie Hatley on Sun Feb 16, 2025.

--------------------------------------------------------------------------------------------------------------
PROBLEM DESCRIPTION:
Task 309-1: Min Gap
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints, increasing order.
Write a script to return the element before which you find the
smallest gap.

Example #1:
Input: @ints = (2, 8, 10, 11, 15)
Output: 11
8 - 2 => 6
10 - 8 => 2
11 - 10 => 1
15 - 11 => 4
11 is where we found the min gap.

Example #2:
Input: @ints = (1, 5, 6, 7, 14)
Output: 6
5 - 1 => 4
6 - 5 => 1
7 - 6 => 1
14 - 7 => 7
6 and 7 where we found the min gap, so we pick the first
instance.

Example #3:
Input: @ints = (8, 20, 25, 28)
Output: 28
8 - 20 => 14
25 - 20 => 5
28 - 25 => 3
28 is where we found the min gap.

--------------------------------------------------------------------------------------------------------------
PROBLEM NOTES:
I'll use a ranged loop to subtract from each-element-but-the-first the previous element and keep track of the
element before-which was the smallest gap.

--------------------------------------------------------------------------------------------------------------
IO NOTES:
Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
single-quoted array of increasing arrays of integers, in proper Perl syntax, like so:
./ch-1.pl '([-18,88,137,148,154,243,363],[3,5,8,13,21,34])'

Output is to STDOUT and will be each input followed by the corresponding output.

=cut

# ------------------------------------------------------------------------------------------------------------
# PRAGMAS, MODULES, AND SUBS:

use v5.36;
# Which element of an increasing sequence of integers
# has the minimum gap from the previous element?
sub min_gap ($aref) {
my $min_ele = $$aref[1];
my $min_gap = $$aref[1] - $$aref[0];
for my $i (2..$#$aref) {
my $ele = $$aref[$i];
my $gap = $$aref[$i] - $$aref[$i-1];
if ($gap < $min_gap) {
$min_ele = $ele;
$min_gap = $gap
}
}
$min_ele;
}

# ------------------------------------------------------------------------------------------------------------
# INPUTS:
my @arrays = @ARGV ? eval($ARGV[0]) : ([2, 8, 10, 11, 15], [1, 5, 6, 7, 14], [8, 20, 25, 28]);
# Expected outputs : 11 6 28

# ------------------------------------------------------------------------------------------------------------
# MAIN BODY OF PROGRAM:
$"=', ';
for my $aref (@arrays) {
say '';
my $mg = min_gap($aref);
say "Array = (@$aref)";
say "Element with minimum gap from previous = $mg";
}
81 changes: 81 additions & 0 deletions challenge-309/robbie-hatley/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env perl

=pod

--------------------------------------------------------------------------------------------------------------
TITLE AND ATTRIBUTION:
Solutions in Perl for The Weekly Challenge 309-2,
written by Robbie Hatley on Sun Feb 16, 2025.

--------------------------------------------------------------------------------------------------------------
PROBLEM DESCRIPTION:
Task 309-2: Min Diff
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints. Write a script to
find the minimum difference between any two elements.

Example #1:
Input: @ints = (1, 5, 8, 9)
Output: 1
1, 5 => 5 - 1 => 4
1, 8 => 8 - 1 => 7
1, 9 => 9 - 1 => 8
5, 8 => 8 - 5 => 3
5, 9 => 9 - 5 => 4
8, 9 => 9 - 8 => 1

Example #2:
Input: @ints = (9, 4, 1, 7)
Output: 2
9, 4 => 9 - 4 => 5
9, 1 => 9 - 1 => 8
9, 7 => 9 - 7 => 2
4, 1 => 4 - 1 => 3
4, 7 => 7 - 4 => 3
1, 7 => 7 - 1 => 6

--------------------------------------------------------------------------------------------------------------
PROBLEM NOTES:
I'll use a pair of nested ranged loops to compare each element to those after it, and return the minimum of
the absolute values of the differences.

--------------------------------------------------------------------------------------------------------------
IO NOTES:
Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
single-quoted array of arrays of real numbers, in proper Perl syntax, like so:
./ch-2.pl '([88.2,37.4,-148.9,243.8,36.3],[-40,20,-20,0,40])'

Output is to STDOUT and will be each input followed by the corresponding output.

=cut

# ------------------------------------------------------------------------------------------------------------
# PRAGMAS, MODULES, AND SUBS:

use v5.36;
use List::Util 'min';
# What is the minimum difference between any
# two elements of an an array of real numbers?
sub min_diff ($aref) {
my @diff = ();
for my $i (0..$#$aref-1) {
for my $j ($i+1..$#$aref) {
push @diff, abs $$aref[$i]-$$aref[$j];
}
}
min @diff;
}

# ------------------------------------------------------------------------------------------------------------
# INPUTS:
my @arrays = @ARGV ? eval($ARGV[0]) : ([1,5,8,9],[9,4,1,7]);

# ------------------------------------------------------------------------------------------------------------
# MAIN BODY OF PROGRAM:
$"=', ';
for my $aref (@arrays) {
say '';
my $md = min_diff ($aref);
say "Array = (@$aref)";
say "min diff = $md";
}