Perl Weekly Challenge 135: in a rush

One way to let me improve my knowledge about Raku (aka Perl 6) is to implement programs in it. Unluckily, I don’t have any production code to implement in Raku yet (sob!). So, why not try solving the Perl Weekly Challenge tasks?

In the following, the assigned tasks for Challenge 135.

PWC 135 - Task 1

The first task was about finding the three middle digits in a given number, assuming it has an odd number of digits. This is implemented as:

sub MAIN( Int $n ) {
    $n.abs.say and exit if $n.Str.chars == 3;
    'too short'.say and exit if $n.Str.chars < 3;
    'even number of digits'.say and exit if $n.Str.chars %% 2;

    my $remove = ( $n.Str.chars - 3 ) / 2;
    $n.Str.comb[ $remove ..^ $remove + 3 ].join.say;

}



The $remove variable contains how many character it is need to remove from the left and right of the digit, and then I comb the input digit and slice it, re-join and print.

PWC 135 - Task 2

The second task was about validating SEDOL strings. It took me more to understand the alghoritm than to implement it.

sub MAIN( Str $SEDOL ) {

    my %map-of-digits;
    for flat 0 ..9, 'A' .. 'Z' {
        %map-of-digits{ $_ } = $++;
    }

    my @weights     = 1, 3, 1, 7, 3, 9;
    my @values      = %map-of-digits{ $SEDOL.comb };
    my $sum         = [+] @values Z* @weights;
    my $check_digit = (10 - $sum % 10) % 10;

    "1".say and exit if $SEDOL ~~ / $check_digit $ /;
    0.say;

}



The %map-of-digits is an hash where each digit is used as key, and the value is the digit to be used in the sum. The @values extracts the values to be summed using the comb of the input string, so each key in the hash. Then summatory is made using the [+] reduction operator on the zip multiplication of each value for its weigth. The the $check_digit is computed as the alghoritm wants. Last, if the input string is ending with the check digit, the program prints 1 and exits, otherwise it prints 0 and terminates.

The article Perl Weekly Challenge 135: in a rush has been posted by Luca Ferrari on October 18, 2021