Skip to content

Commit 1bfa161

Browse files
authored
Merge pull request #9904 from robbie-hatley/rh264
Robbie Hatley's Perl solutions for The Weekly Challenge #264.
2 parents 2f9387d + cacbf4e commit 1bfa161

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

challenge-264/robbie-hatley/blog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://hatley-software.blogspot.com/2024/04/robbie-hatleys-solutions-to-weekly_9.html
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env perl
2+
3+
=pod
4+
5+
--------------------------------------------------------------------------------------------------------------
6+
TITLE AND ATTRIBUTION:
7+
Solutions in Perl for The Weekly Challenge 264-1,
8+
written by Robbie Hatley on Mon Apr 08, 2024.
9+
10+
--------------------------------------------------------------------------------------------------------------
11+
PROBLEM DESCRIPTION:
12+
Task 264-1: Greatest English Letter
13+
Submitted by: Mohammad Sajid Anwar
14+
You are given a string, $str, made up of only alphabetic
15+
characters [a..zA..Z]. Write a script to return the greatest
16+
english letter in the given string. (A letter is "greatest" if
17+
it occurs as lower and upper case. Also letter ‘b’ is greater
18+
than ‘a’ if ‘b’ appears after ‘a’ in the English alphabet.
19+
20+
Example 1:
21+
Input: $str = 'PeRlwEeKLy'
22+
Output: L
23+
There are two letters E and L that appears as lower and upper.
24+
The letter L appears after E, so the L is the greatest english
25+
letter.
26+
27+
Example 2:
28+
Input: $str = 'ChaLlenge'
29+
Output: L
30+
31+
Example 3:
32+
Input: $str = 'The'
33+
Output: ''
34+
35+
--------------------------------------------------------------------------------------------------------------
36+
PROBLEM NOTES:
37+
My approach to solving this problem will be:
38+
1. Make a list "@chars" of all characters from the given input string "$str".
39+
2. Iterate through that list from left to right, keeping track of the "greatest" character found so-far
40+
which appears as both lower-case and UPPER-CASE in $str.
41+
3. Return the greatest character found (or return an empty string if no great characters were found).
42+
43+
--------------------------------------------------------------------------------------------------------------
44+
IO NOTES:
45+
Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
46+
single-quoted array of double-quoted strings of miXeD-CaSE English LeTTeRS, in proper Perl syntax, like so:
47+
./ch-1.pl '("I have \$73.50!", "SheshivaAr", "FredBobSTEVE", "bAthuQuoMu")'
48+
49+
Output is to STDOUT and will be each input followed by the corresponding output.
50+
51+
=cut
52+
53+
# ------------------------------------------------------------------------------------------------------------
54+
# PRAGMAS, MODULES, AND SUBS:
55+
56+
use v5.36;
57+
use strict;
58+
use warnings;
59+
use List::MoreUtils 'any';
60+
61+
# Return "Greatest English Letter" from a given string
62+
# (or an empty string if there are no "great" letters):
63+
sub gel ($str) {
64+
my @chars = split //, $str;
65+
my $greatest = '';
66+
foreach my $char (@chars) {
67+
if ( any {$_ eq ($char =~ y/A-Za-z/a-zA-Z/r) } @chars ) {
68+
if ( uc($char) gt $greatest ) {
69+
$greatest = uc($char);
70+
}
71+
}
72+
}
73+
return $greatest;
74+
}
75+
76+
# ------------------------------------------------------------------------------------------------------------
77+
# INPUTS:
78+
my @strings = @ARGV ? eval($ARGV[0]) :
79+
(
80+
# Example 1 Input:
81+
'PeRlwEeKLy',
82+
# Expected Output: 'L'
83+
84+
# Example 2 Input:
85+
'ChaLlenge',
86+
# Expected Output: 'L'
87+
88+
# Example 3:
89+
'The',
90+
# Expected Output: ''
91+
);
92+
93+
# ------------------------------------------------------------------------------------------------------------
94+
# MAIN BODY OF PROGRAM:
95+
for my $str (@strings) {
96+
say '';
97+
say "Input string = '$str'";
98+
$str !~ m/^[A-Za-z]+$/ and say "Invalid string." and next;
99+
my $gel = gel($str);
100+
say "Greatest English Letter = '$gel'";
101+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env perl
2+
3+
=pod
4+
5+
--------------------------------------------------------------------------------------------------------------
6+
TITLE AND ATTRIBUTION:
7+
Solutions in Perl for The Weekly Challenge 264-2,
8+
written by Robbie Hatley on Mon Apr 08, 2024.
9+
10+
--------------------------------------------------------------------------------------------------------------
11+
PROBLEM DESCRIPTION:
12+
Task 264-2: Target Array
13+
Submitted by: Mohammad Sajid Anwar
14+
You are given two arrays of integers, @source and @indices.
15+
The @indices can only contains integers 0 <= i < size of @source.
16+
Write a script to create target array by inserting at index
17+
$indices[i] the value $source[i].
18+
19+
Example 1:
20+
Input: @source = (0, 1, 2, 3, 4)
21+
@indices = (0, 1, 2, 2, 1)
22+
Output: (0, 4, 1, 3, 2)
23+
@source @indices @target
24+
0 0 (0)
25+
1 1 (0, 1)
26+
2 2 (0, 1, 2)
27+
3 2 (0, 1, 3, 2)
28+
4 1 (0, 4, 1, 3, 2)
29+
30+
Example 2:
31+
Input: @source = (1, 2, 3, 4, 0)
32+
@indices = (0, 1, 2, 3, 0)
33+
Output: (0, 1, 2, 3, 4)
34+
@source @indices @target
35+
1 0 (1)
36+
2 1 (1, 2)
37+
3 2 (1, 2, 3)
38+
4 3 (1, 2, 3, 4)
39+
0 0 (0, 1, 2, 3, 4)
40+
41+
Example 3:
42+
Input: @source = (1)
43+
@indices = (0)
44+
Output: (1)
45+
46+
--------------------------------------------------------------------------------------------------------------
47+
PROBLEM NOTES:
48+
This is one of those few Weekly Challenges where Task 2 is simpler than Task 1 (usually it's the other way
49+
around). I'll solve this problem by using Perl's built-in "splice" feature to "splice" desired elements from
50+
the "source" array into the target array being constructed, using the indices from the "indices" array to
51+
specify insertion points.
52+
53+
--------------------------------------------------------------------------------------------------------------
54+
IO NOTES:
55+
Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
56+
single-quoted array of arrays of two arrays of integers, with the second array of each inner pair being
57+
insertion indices for the first array, in proper Perl syntax, like so:
58+
./ch-2.pl '([[18,32,74],[0,0,0,]],[[18,32,74],[0,1,2]])'
59+
60+
Output is to STDOUT and will be each input followed by the corresponding output.
61+
62+
=cut
63+
64+
# ------------------------------------------------------------------------------------------------------------
65+
# PRAGMAS, MODULES, AND SUBS:
66+
67+
use v5.36;
68+
use strict;
69+
use warnings;
70+
sub target_array ($s, $i) {
71+
my @t = ();
72+
for ( my $ii=0 ; $ii <= $#$s ; ++$ii ) {
73+
splice @t, $$i[$ii], 0, $$s[$ii];
74+
}
75+
return @t;
76+
}
77+
78+
# ------------------------------------------------------------------------------------------------------------
79+
# INPUTS:
80+
my @arrays = @ARGV ? eval($ARGV[0]) :
81+
(
82+
# Example 1 Input:
83+
[
84+
[0, 1, 2, 3, 4],
85+
[0, 1, 2, 2, 1],
86+
],
87+
# Expected Output: (0, 4, 1, 3, 2)
88+
89+
# Example 2 Input:
90+
[
91+
[1, 2, 3, 4, 0],
92+
[0, 1, 2, 3, 0],
93+
],
94+
# Expected Output: (0, 1, 2, 3, 4)
95+
96+
# Example 3 Input:
97+
[
98+
[1],
99+
[0],
100+
],
101+
# Expected Output: (1)
102+
);
103+
104+
# ------------------------------------------------------------------------------------------------------------
105+
# MAIN BODY OF PROGRAM:
106+
for my $aref (@arrays) {
107+
say '';
108+
say 'Source array = (', join(', ', @{$aref->[0]}), ')';
109+
say 'Indices array = (', join(', ', @{$aref->[1]}), ')';
110+
my @target = target_array($aref->[0], $aref->[1]);
111+
say 'Target array = (', join(', ', @target ), ')';
112+
}

0 commit comments

Comments
 (0)