/
ch-1.pl
172 lines (128 loc) · 3.98 KB
/
ch-1.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!perl
################################################################################
=comment
Perl Weekly Challenge 264
=========================
TASK #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: ''
=cut
################################################################################
#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. A single string, containing alphabetic characters only, is given on the
command-line.
=cut
#===============================================================================
use v5.32.1; # Enables strictures
use warnings;
use Const::Fast;
use Test::More;
const my $USAGE => <<END;
Usage:
perl $0 <str>
perl $0
<str> A string containing alphabetic characters (A-Z, a-z) only
END
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 264, Task #1: Greatest English Letter (Perl)\n\n";
}
#===============================================================================
MAIN:
#===============================================================================
{
my $argc = scalar @ARGV;
if ($argc == 0)
{
run_tests();
}
elsif ($argc == 1)
{
my $str = $ARGV[ 0 ];
$str =~ / ^ [[:alpha:]]* $ /x
or error( qq[Invalid input string "$str"] );
print "Input: \$str = '$str'\n";
my $gel = find_greatest_English_letter( $str );
print "Output: '$gel'\n";
}
else
{
error( qq[Expected 1 or 0 command-line arguments, found $argc] );
}
}
#-------------------------------------------------------------------------------
sub find_greatest_English_letter
#-------------------------------------------------------------------------------
{
my ($str) = @_;
my %letters;
++$letters{ $_ } for split //, $str;
my $gel = '';
my @uc = grep { / ^ [[:upper:]] $ /x } keys %letters;
for (sort { $b cmp $a } @uc) # Sort descending
{
if (exists $letters{ lc() })
{
$gel = $_;
last;
}
}
return $gel;
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $str, $expected) = split / \| /x, $line;
for ($test_name, $str, $expected)
{
s/ ^ \s+ //x;
s/ \s+ $ //x;
}
my $gel = find_greatest_English_letter( $str );
is $gel, $expected, $test_name;
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
################################################################################
__DATA__
Example 1|PeRlwEeKLy|L
Example 2|ChaLlenge |L
Example 3|The |