Task 1: Max Str Value
"String Showdown: Who Wins the Value Battle?"
Find the max value of an alphanumeric string array - use numeric value if digits only, otherwise use string length.
The Strategy: For each string, check if it contains only digits. If so, convert to integer; otherwise use length. Track the maximum.
Perl Implementation
sub max_str_val {
my @str = @_;
my $max = 0;
for (@str) {
my $val = /^\d+$/ ? $_ : length $_;
$max = $val if $val > $max;
}
return $max;
}
Python Implementation
def max_str_val(strings: list[str]) -> int:
"""Find max value: numeric if all digits, else length."""
max_val = 0
for s in strings:
if s.isdigit():
val = int(s)
else:
val = len(s)
max_val = max(max_val, val)
return max_val