Raku challenges Week 2

Task 1. Remove leading zeros from positive numbers.

Task 2. Convert a number to the one represented as a base-35 number and back.

The solutions in the Raku programming language of the challenges offered on Week 2 of the Perl Weekly Challenges.

Table of Contents

Task 1

Remove leading zeros from positive numbers.

So, we need to take a string such as 000042 and print it as 42. Even here, there are two obvious solutions:

my $n = '00000042';

say +$n;
say $n.Int;

A prefix form of + converts the string to an integer. The same can be done by an explicit call of the Int method.

The program prints the expected number:

$ raku ch-1.raku
42
42

Task 2

Convert a number to the one represented as a base-35 number and back.

In Raku, there is a pair of routines, base and parse-base. They are ideal tools for solving the task.

To convert the number, use base:

my $n = 2020;
my $n35 = $n.base(35);

say $n35;

This prints 1MP.

To convert it back to decimal system, use parse-base:

my $n10 = $n35.parse-base(35);
say $n10;

After this operation, the program prints the initial number 2020.

Look for the source files in the folder 002 of the 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