Perl Weekly Challenge 250.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 250.
Task 1: Smallest Index
Submitted by: Mohammad S Anwar
You are given an array of integers, @ints.
Write a script to find the smallest index i such that i mod 10 == $ints[i]
otherwise return -1.
Example 1
Input: @ints = (0, 1, 2)
Output: 0
i=0: 0 mod 10 = 0 == $ints[0].
i=1: 1 mod 10 = 1 == $ints[1].
i=2: 2 mod 10 = 2 == $ints[2].
All indices have i mod 10 == $ints[i], so we return the smallest index 0.
Example 2
Input: @ints = (4, 3, 2, 1)
Output: 2
i=0: 0 mod 10 = 0 != $ints[0].
i=1: 1 mod 10 = 1 != $ints[1].
i=2: 2 mod 10 = 2 == $ints[2].
i=3: 3 mod 10 = 3 != $ints[3].
2 is the only index which has i mod 10 == $ints[i].
Example 3
Input: @ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Output: -1
Explanation: No index satisfies i mod 10 == $ints[i].
Straightforward one-liner, just follow the description using first
from List::Util
.
Example 1:
perl -MList::Util=first -E '@x=@ARGV; say "@x -> ", (first {$x[$_]%10==$_} @x)//-1' 0 1 2
Results:
0 1 2 -> 0
Example 2:
perl -MList::Util=first -E '@x=@ARGV; say "@x -> ", (first {$x[$_]%10==$_} @x)//-1' 4 3 2 1
Results:
4 3 2 1 -> 2
Example 3:
perl -MList::Util=first -E '@x=@ARGV; say "@x -> ", (first {$x[$_]%10==$_} @x)//-1' 1 2 3 4 5 6 7 8 9 0
Results:
1 2 3 4 5 6 7 8 9 0 -> -1
The full code is almost identical.
1 # Perl weekly challenge 250
2 # Task 1: Smallest Index
3 #
4 # See https://wlmb.github.io/2024/01/01/PWC250/#task-1-smallest-index
5 use v5.36;
6 use List::Util qw(first);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 N0 [N1...]
9 to find first index i for which N_i%10==i
10 FIN
11 say "@ARGV -> ", (first {$ARGV[$_]%10==$_} @ARGV)//-1;
Examples:
./ch-1.pl 0 1 2
./ch-1.pl 4 3 2 1
./ch-1.pl 1 2 3 4 5 6 7 8 9 0
Results:
0 1 2 -> 0
4 3 2 1 -> 2
1 2 3 4 5 6 7 8 9 0 -> -1
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
Again, straightforward solution by just following the directions. I allow a sign in the numeric values.
Example 1:
perl -MList::Util=max -E '
@x=@ARGV; say "@x -> ", max map {/^[+-]?\d+$/?0+$_:length $_} @x;' perl 2 000 python r4ku
Results:
perl 2 000 python r4ku -> 6
Example 2:
perl -MList::Util=max -E '
@x=@ARGV; say "@x -> ", max map {/^[+-]?\d+$/?0+$_:length $_} @x;' 001 1 000 0001
Results:
001 1 000 0001 -> 1
The full code is identical:
1 # Perl weekly challenge 250
2 # Task 2: Alphanumeric String Value
3 #
4 # See https://wlmb.github.io/2024/01/01/PWC250/#task-2-alphanumeric-string-value
5 use v5.36;
6 use List::Util qw(max);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 S0 [S1...]
9 to find the maximum \"value\" of the strings S0 S1...
10 FIN
11 say "@ARGV -> ", max map {/^[+-]?\d+$/?0+$_:length $_} @ARGV;
Examples:
./ch-2.pl perl 2 000 python r4ku
./ch-2.pl 001 1 000 0001
Results:
perl 2 000 python r4ku -> 6
001 1 000 0001 -> 1
/;
Written on January 1, 2024