Skip to content

Robbie Hatley's Perl solutions for The Weekly Challenge #264. #9904

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 1 commit into from
Apr 9, 2024
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-264/robbie-hatley/blog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://hatley-software.blogspot.com/2024/04/robbie-hatleys-solutions-to-weekly_9.html
101 changes: 101 additions & 0 deletions challenge-264/robbie-hatley/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env perl

=pod

--------------------------------------------------------------------------------------------------------------
TITLE AND ATTRIBUTION:
Solutions in Perl for The Weekly Challenge 264-1,
written by Robbie Hatley on Mon Apr 08, 2024.

--------------------------------------------------------------------------------------------------------------
PROBLEM DESCRIPTION:
Task 264-1: Greatest English Letter
Submitted by: Mohammad Sajid Anwar
You are given a string, $str, made up of only alphabetic
characters [a..zA..Z]. Write a script to return the greatest
english letter in the given string. (A letter is "greatest" if
it occurs as lower and upper case. Also letter ‘b’ is greater
than ‘a’ if ‘b’ appears after ‘a’ in the English alphabet.

Example 1:
Input: $str = 'PeRlwEeKLy'
Output: L
There are two letters E and L that appears as lower and upper.
The letter L appears after E, so the L is the greatest english
letter.

Example 2:
Input: $str = 'ChaLlenge'
Output: L

Example 3:
Input: $str = 'The'
Output: ''

--------------------------------------------------------------------------------------------------------------
PROBLEM NOTES:
My approach to solving this problem will be:
1. Make a list "@chars" of all characters from the given input string "$str".
2. Iterate through that list from left to right, keeping track of the "greatest" character found so-far
which appears as both lower-case and UPPER-CASE in $str.
3. Return the greatest character found (or return an empty string if no great characters were found).

--------------------------------------------------------------------------------------------------------------
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 double-quoted strings of miXeD-CaSE English LeTTeRS, in proper Perl syntax, like so:
./ch-1.pl '("I have \$73.50!", "SheshivaAr", "FredBobSTEVE", "bAthuQuoMu")'

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

=cut

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

use v5.36;
use strict;
use warnings;
use List::MoreUtils 'any';

# Return "Greatest English Letter" from a given string
# (or an empty string if there are no "great" letters):
sub gel ($str) {
my @chars = split //, $str;
my $greatest = '';
foreach my $char (@chars) {
if ( any {$_ eq ($char =~ y/A-Za-z/a-zA-Z/r) } @chars ) {
if ( uc($char) gt $greatest ) {
$greatest = uc($char);
}
}
}
return $greatest;
}

# ------------------------------------------------------------------------------------------------------------
# INPUTS:
my @strings = @ARGV ? eval($ARGV[0]) :
(
# Example 1 Input:
'PeRlwEeKLy',
# Expected Output: 'L'

# Example 2 Input:
'ChaLlenge',
# Expected Output: 'L'

# Example 3:
'The',
# Expected Output: ''
);

# ------------------------------------------------------------------------------------------------------------
# MAIN BODY OF PROGRAM:
for my $str (@strings) {
say '';
say "Input string = '$str'";
$str !~ m/^[A-Za-z]+$/ and say "Invalid string." and next;
my $gel = gel($str);
say "Greatest English Letter = '$gel'";
}
112 changes: 112 additions & 0 deletions challenge-264/robbie-hatley/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env perl

=pod

--------------------------------------------------------------------------------------------------------------
TITLE AND ATTRIBUTION:
Solutions in Perl for The Weekly Challenge 264-2,
written by Robbie Hatley on Mon Apr 08, 2024.

--------------------------------------------------------------------------------------------------------------
PROBLEM DESCRIPTION:
Task 264-2: Target Array
Submitted by: Mohammad Sajid Anwar
You are given two arrays of integers, @source and @indices.
The @indices can only contains integers 0 <= i < size of @source.
Write a script to create target array by inserting at index
$indices[i] the value $source[i].

Example 1:
Input: @source = (0, 1, 2, 3, 4)
@indices = (0, 1, 2, 2, 1)
Output: (0, 4, 1, 3, 2)
@source @indices @target
0 0 (0)
1 1 (0, 1)
2 2 (0, 1, 2)
3 2 (0, 1, 3, 2)
4 1 (0, 4, 1, 3, 2)

Example 2:
Input: @source = (1, 2, 3, 4, 0)
@indices = (0, 1, 2, 3, 0)
Output: (0, 1, 2, 3, 4)
@source @indices @target
1 0 (1)
2 1 (1, 2)
3 2 (1, 2, 3)
4 3 (1, 2, 3, 4)
0 0 (0, 1, 2, 3, 4)

Example 3:
Input: @source = (1)
@indices = (0)
Output: (1)

--------------------------------------------------------------------------------------------------------------
PROBLEM NOTES:
This is one of those few Weekly Challenges where Task 2 is simpler than Task 1 (usually it's the other way
around). I'll solve this problem by using Perl's built-in "splice" feature to "splice" desired elements from
the "source" array into the target array being constructed, using the indices from the "indices" array to
specify insertion points.

--------------------------------------------------------------------------------------------------------------
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 two arrays of integers, with the second array of each inner pair being
insertion indices for the first array, in proper Perl syntax, like so:
./ch-2.pl '([[18,32,74],[0,0,0,]],[[18,32,74],[0,1,2]])'

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

=cut

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

use v5.36;
use strict;
use warnings;
sub target_array ($s, $i) {
my @t = ();
for ( my $ii=0 ; $ii <= $#$s ; ++$ii ) {
splice @t, $$i[$ii], 0, $$s[$ii];
}
return @t;
}

# ------------------------------------------------------------------------------------------------------------
# INPUTS:
my @arrays = @ARGV ? eval($ARGV[0]) :
(
# Example 1 Input:
[
[0, 1, 2, 3, 4],
[0, 1, 2, 2, 1],
],
# Expected Output: (0, 4, 1, 3, 2)

# Example 2 Input:
[
[1, 2, 3, 4, 0],
[0, 1, 2, 3, 0],
],
# Expected Output: (0, 1, 2, 3, 4)

# Example 3 Input:
[
[1],
[0],
],
# Expected Output: (1)
);

# ------------------------------------------------------------------------------------------------------------
# MAIN BODY OF PROGRAM:
for my $aref (@arrays) {
say '';
say 'Source array = (', join(', ', @{$aref->[0]}), ')';
say 'Indices array = (', join(', ', @{$aref->[1]}), ')';
my @target = target_array($aref->[0], $aref->[1]);
say 'Target array = (', join(', ', @target ), ')';
}