diff options
| author | Bob Lied <boblied+github@gmail.com> | 2024-01-01 07:32:42 -0600 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2024-01-01 07:32:42 -0600 |
| commit | 44c8693d5c2ee99d39cd0f1a929454d426b0d12e (patch) | |
| tree | 7b5cc349230dc300942454875aa7db7af2c62bd3 | |
| parent | 9ce5d954d0e8633492d97451dcaa3f93c103831f (diff) | |
| download | perlweeklychallenge-club-44c8693d5c2ee99d39cd0f1a929454d426b0d12e.tar.gz perlweeklychallenge-club-44c8693d5c2ee99d39cd0f1a929454d426b0d12e.tar.bz2 perlweeklychallenge-club-44c8693d5c2ee99d39cd0f1a929454d426b0d12e.zip | |
Week 250 Task 2 done
| -rw-r--r-- | challenge-250/bob-lied/perl/ch-2.pl | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-250/bob-lied/perl/ch-2.pl b/challenge-250/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..a697126c24 --- /dev/null +++ b/challenge-250/bob-lied/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# +# ch-2.pl Perl Weekly Challenge 250 Task 2 Alphanumeric String Value +#============================================================================= +# 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 +# Example 2 Input: @alphanumstr = ("001", "1", "000", "0001") +# Output: 1 +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say alnumstr(@ARGV); + +sub alnumstr(@str) +{ + use List::Util qw/max/; + return max map { $_ =~ m/\A\d+\Z/a ? 0+$_ : length($_) } @str; +} + +sub runTest +{ + use Test2::V0; + + is( alnumstr("perl", "2", "000", "python", "r4ku"), 6, "Example 1"); + is( alnumstr( "001", "1", "000", "0001" ), 1, "Example 1"); + + is( alnumstr( "017", "016"), 17, "Octal?"); + + is( alnumstr( "03", "02", "0x1"), 3, "Hex"); + + done_testing; +} |
