diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-04-09 01:50:53 -0700 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-04-09 01:50:53 -0700 |
| commit | cacbf4e82f67f7af2354969d3ee84558e6f0e550 (patch) | |
| tree | 144d012aeb61f84035faa8940d75a8eb2fe52a01 | |
| parent | f14e99c374e42f272a7e43324e5ac195996c9a54 (diff) | |
| download | perlweeklychallenge-club-cacbf4e82f67f7af2354969d3ee84558e6f0e550.tar.gz perlweeklychallenge-club-cacbf4e82f67f7af2354969d3ee84558e6f0e550.tar.bz2 perlweeklychallenge-club-cacbf4e82f67f7af2354969d3ee84558e6f0e550.zip | |
Robbie Hatley's Perl solutions for The Weekly Challenge #264.
| -rw-r--r-- | challenge-264/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-264/robbie-hatley/perl/ch-1.pl | 101 | ||||
| -rwxr-xr-x | challenge-264/robbie-hatley/perl/ch-2.pl | 112 |
3 files changed, 214 insertions, 0 deletions
diff --git a/challenge-264/robbie-hatley/blog.txt b/challenge-264/robbie-hatley/blog.txt new file mode 100644 index 0000000000..6dfcda6b04 --- /dev/null +++ b/challenge-264/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2024/04/robbie-hatleys-solutions-to-weekly_9.html
\ No newline at end of file diff --git a/challenge-264/robbie-hatley/perl/ch-1.pl b/challenge-264/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..4c61b376a4 --- /dev/null +++ b/challenge-264/robbie-hatley/perl/ch-1.pl @@ -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'"; +} diff --git a/challenge-264/robbie-hatley/perl/ch-2.pl b/challenge-264/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..4f80e20475 --- /dev/null +++ b/challenge-264/robbie-hatley/perl/ch-2.pl @@ -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 ), ')'; +} |
