/
ch-2.pl
167 lines (124 loc) · 4.14 KB
/
ch-2.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
#!perl
################################################################################
=comment
Perl Weekly Challenge 250
=========================
TASK #2
-------
*Alphanumeric String Value*
Submitted by: Mohammad S Anwar
You are given an array of alphanumeric strings.
Write a script to return the maximum value of alphanumeric string in the given
array.
The value of alphanumeric string can be defined as
a) The numeric representation of the string in base 10 if it is made up of
digits only.
b) otherwise the length of the string
Example 1
Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku")
Output: 6
"perl" consists of letters only so the value is 4.
"2" is digits only so the value is 2.
"000" is digits only so the value is 0.
"python" consits of letters so the value is 6.
"r4ku" consists of letters and digits so the value is 4.
Example 2
Input: @alphanumstr = ("001", "1", "000", "0001")
Output: 1
=cut
################################################################################
#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Assumptions
-----------
1. An "alphanumeric string" contains letters and/or digits only. So, "+", "-",
"_", etc., are not allowed.
2. An empty string has no digits, therefore its value is its length (viz. 0).
Interface
---------
If no command-line arguments are given, the test suite is run.
=cut
#===============================================================================
use v5.32.1; # Enables strictures
use warnings;
use Const::Fast;
use Test::More;
const my $USAGE => <<END;
Usage:
perl $0 [<alphanumstr> ...]
perl $0
[<alphanumstr> ...] A non-empty list of alphanumeric strings
END
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 250, Task #2: Alphanumeric String Value (Perl)\n\n";
}
#===============================================================================
MAIN:
#===============================================================================
{
if (scalar @ARGV == 0)
{
run_tests();
}
else
{
my @alphanumstr = @ARGV;
/ ^ [A-Za-z0-9]* $ /x or error( qq[Invalid input string "$_"] )
for @alphanumstr;
printf "Input: \@alphanumstr = (%s)\n",
join ', ', map { qq["$_"] } @alphanumstr;
my $max_value = find_max_value( \@alphanumstr );
print "Output: $max_value\n";
}
}
#-------------------------------------------------------------------------------
sub find_max_value
#-------------------------------------------------------------------------------
{
my ($alphanumstr) = @_;
my $max_value = 0;
for (@$alphanumstr)
{
my $value = (/ ^ [0-9]+ $ /x ? $_ : length) + 0; # Numify the string
$max_value = $value if $value > $max_value;
}
return $max_value;
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $alphanumstrs, $expected) = split / \| /x, $line;
for ($test_name, $alphanumstrs, $expected)
{
s/ ^ \s+ //x;
s/ \s+ $ //x;
}
my @alphanumstr = split / \s+ /x, $alphanumstrs;
my $max_value = find_max_value( \@alphanumstr );
is $max_value, $expected, $test_name;
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
################################################################################
__DATA__
Example 1|perl 2 000 python r4ku|6
Example 2|001 1 000 0001 |1