Raku Challenge Week 1

Task 1. Capitalise all small letters ‘e’ and count the number of replacements in the string ‘Perl Weekly Challenge.’

Task 2. Write a one-liner that, for the numbers from 1 to 20, prints ‘fizz’ if the number is divisible by 3 and ‘buzz’ if it is divisible by 5.

Here are my solutions to the first week of the challenge. Well, written after 67 weeks after it was published.

Table of Contents

Task 1

Capitalise all small letters ‘e’ and count the number of replacements in the string ‘Perl Weekly Challenge.’

Nothing fancy with the solution. Raku, as well as Perl, is good for such things.

my $s = 'Perl Weekly Challenge';
$s ~~ s:g/e/E/;
say $/.elems;
say $s;

The program prints:

$ raku ch-1.raku
5
PErl WEEkly ChallEngE

I think, one element needs a comment. The number of replacements is computed as $/.elems, which is the number of the elements in the $/ variable, which, after the regex match, has the following content:

(「e」 「e」 「e」 「e」 「e」)

Task 2

Write a one-liner that, for the numbers from 1 to 20, prints ‘fizz’ if the number is divisible by 3 and ‘buzz’ if it is divisible by 5.

This is a typical interview task which tests if you think of the numbers such as 15, which are divisible by both 3 and 5.

Let me offer an extremely straightforward solution:

say "$_ ", 
       $_ %% 15 ?? 'fizzbuzz' 
    !! $_ %% 3  ?? 'fizz' 
    !! $_ %% 5  ?? 'buzz' 
    !! ''
    for 1..20;

Output:

$ raku ch-2.raku 
1 
2 
3 fizz
4 
5 buzz
6 fizz
7 
8 
9 fizz
10 buzz
11 
12 fizz
13 
14 
15 fizzbuzz
16 
17 
18 fizz
19 
20 buzz

Find the source code in the directory 001 in my GitHub repository.

GitHub repository
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