Practicing Raku challenges

A few Raku challenges solutions: printing arrays, generating Leonardo numbers, converting to octals.

Hi, let me demonstrate a few solutions of the tasks from the Perl Weekly Challenge from the past weeks.

Week 040, issue 1

Task: You are given two or more arrays. Write a script to display values of each list at a given index.

Here, I am using the same technique of looping over several arrays that I found out during the work on covid.observer:

my @a = < I L O V E Y O U >;
my @b = < 2 4 0 3 2 0 1 9 >;
my @c = < ! ? £ $ % ^ & * >;

for @a Z @b Z @c -> ($a, $b, $c) {
    say "$a $b $c";
}

The program emits the following output:

$ raku ch-040-1.raku
I 2 !
L 4 ?
O 0 £
V 3 $
E 2 %
Y 0 ^
O 1 &
U 9 *

Week 041, issue 2

Task: Print the first 20 Leonardo numbers.

The definition of such sequences can be very easily expressed via multi-functions:

multi sub L(0) { 1 }
multi sub L(1) { 1 }

multi sub L($n) {
    L($n - 1) + L($n - 2) + 1
}

say L($_) for ^20;

The program prints the first Leonardo numbers:

$ raku ch-041-2.raku 
1
1
3
5
9
15
25
41
67
109
177
287
465
753
1219
1973
3193
5167
8361
13529

Week 042, issue 1

Task: Print the numbers from 0 to 50 in octal format.

There are a few ways of how to convert the number to the number with the given base in Raku, here is one of the simplest ones:

say "Decimal $_ = Octal {$_.base(8)}" for ^50;

The output stars with the following lines:

$ raku ch-042-1.raku
Decimal 0 = Octal 0
Decimal 1 = Octal 1
Decimal 2 = Octal 2
Decimal 3 = Octal 3
Decimal 4 = Octal 4
Decimal 5 = Octal 5
Decimal 6 = Octal 6
Decimal 7 = Octal 7
Decimal 8 = Octal 10
Decimal 9 = Octal 11
Decimal 10 = Octal 12
. . .

You can find more solutions by browsing the Raku challenges category on my site. The solutions from today’s post are available on GitHub.

Navigation to the Raku challenges post series

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code