Perl Weekly Challenge 308: lazyness

This post presents my solutions to the Perl Weekly Challenge 308.
I keep doing the Perl Weekly Challenge in order to mantain my coding skills in good shape, as well as in order to learn new things, with particular regard to Raku, a language that I love.
This week, I solved the following tasks:

Raku Implementations

PWC 308 - Task 1 - Raku Implementation

The first task was about counting how many same words are present in two different given arrays.

sub MAIN(  :@left,  :@right ) {

    my @matches;

    for @left -> $left {
		@matches.push: $left if ( @right.grep( * ~~ $left ) );
    }

    @matches.elems.say;
}



It is simple to solve with a grep and an iteration.

PWC 308 - Task 2 - Raku Implementation

The second task was about to obtain the array that produced an encoded given array given a seed and knowing that each element has been xored with the previous one.

sub MAIN(  :@arr, :$initial  ) {

    my @result = $initial;
    for 1 .. @arr -> $index {
		@result.push: @result[ * - 1 ] +^ @arr[ $index - 1 ];
    }

    @result.say;
}



The trick is to perform the xor operations backwards while producing the original array.

The article Perl Weekly Challenge 308: lazyness has been posted by Luca Ferrari on February 13, 2025