Perl Weekly Challenge 45: encoded messages and self-source-code-printing

PWC 45 - Task 1

% perl6 ch-1.p6 --message = "The quick brown fox jumps over the lazy dog" Your original message is The quick brown fox jumps over the lazy dog and encoded results: t h e q u i c k b r o w n f o x j u m p s o v e r t h e l a z y d o g that leads to tbjrdhrutoeomhgqwpeunslifoacovzkxey

for $message.lc.comb( / \w / ) { @matrix[ $row ].push: $_ if @matrix[ $row ].elems < $columns; $row++ if @matrix[ $row ].elems == $columns; }

comb

$message

push

@matrix[ $row ]

$row

$columns

$columns

for 0 .. $columns -> $start { ( @matrix[ $_ ][ $start ] // '' ).print for 0 .. $row; }

$columns

0 .. $row

A Smaller Solution

rotor

:partial

my @matrix = $message .lc .comb( /\w/ ) .rotor: 8, :partial;

@matrix

@matrix.elems

for 0 .. $columns -> $start { ( @matrix[ $_ ][ $start ] // '' ).print for 0 .. @matrix.elems; }

PWC 45 - Task 2

$*PROGRAM

IO

lines

.say for $*PROGRAM.lines;

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 45 My official solutions are available on the GitHub repository The task asks to get a message, remove all the spaces and distribute the message across an eight column matrix. Finally, print the message by columns instead of rows. This is called the squate secret code.To better understand, let’s consider the following example:First of all, I need to get a matrix with all non spaces chars:Themethod extracts only the letters (non-spaces) from the string, then Ievery single letter into, that is an array of array (indexed by) up until(i.e., 8 columns). Once I hit the(i.e., 8) upper limit, I change the matrix row.Now it’s time to print the matrix by columns:I loop over thefirst, and then I do print the current position between all the matrix rows (i.e., between, with the latter being increased by the preceeding loop). Please note that the matrix could have the last line not completed, so I print a char or an empty char if nothing is defined in that matrix position.I was not satisfied about the above solution, and after a while I remembered that strange method that I seldom use:. This method splits a list into sublists of the specified size, and with theadverb, can include also lists that are not complete. Therefore, the whole first loop can be reduced to:that produces the very sameas the loop before, and moreover, thecorresponds to the number of rows the matrix has. From the above, the printing loop becomes:This has been very easy, a single line script: provide a program that prints its own source code. Since in Raku, the dynamic variableolds anobject, I can simply loop over theand print them out.Either this has been too simple, or I didn’t understand the task assignment!