Perl Weekly Challenge: Week 250

Challenge 1:

Smallest Index

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]

Solving this one is quite straightforward.

$result is initially set to -1 in case we don't find an index that meets the criterion.

my $result = -1;

Then we go through the array indices...

for 0 .. @ints.end -> $i {

If we find one that meets the criterion, we make it the value of $result and stop processing.

    if @ints[$i] % 10 == $i {
        $result = $i;
        last;
    }
}

Finally $result is printed.

say $result;

(Full code on Github.)

This is the Perl version. It works the same as in Raku.

my $result = -1;

for my $i (0 .. scalar @ARGV - 1) {
    if ($ARGV[$i] % 10 == $i) {
        $result = $i;
        last;
    }
}

say $result;

(Full code on Github.)

Challenge 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

"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

Solving this one follows the same plan as challenge 1 with only minor adjustments.

The result will be stored in $maxvalue which is initially set to -∞ the smallest possible number. my $maxvalue = -∞;

Then for every alphanumeric string...

for @alphanumstr -> $str {

As per the spec, if it is all numbers, its value is its' representation otherwise the length of the string.

    my $value = ($str ~~ /^ \d+ $/) ?? $str.base(10) !! $str.chars;

If this value is greater than the current $maxvalue, it becomes the new value of $maxvalue.

    if $value > $maxvalue {
        $maxvalue = $value;
    }
}

Finally, $maxvalue is printed.

say $maxvalue;

(Full code on Github.)

The Perl version works the same as Raku.

my $maxvalue = -Inf;

for my $str (@ARGV) {
    my $value = ($str =~ /^\d+$/) ? 0 + $str : length $str;
    if ($value > $maxvalue) {
        $maxvalue = $value;
    }
}

say $maxvalue;

(Full code on Github.)