By Shahed Nooshmand

Perl Weekly Challenge: week 60

A little quiet, this week.

Task #1

Write a script that accepts a number and returns the Excel Column Name it represents and vice-versa.

Excel columns start at A and increase lexicographically using the 26 letters of the English alphabet, A..Z. After Z, the columns pick up an extra “digit”, going from AA, AB, etc., which could (in theory) continue to an arbitrary number of digits. In practice, Excel sheets are limited to 16,384 columns.

Raku has string sequences. In fact, "A"..* is an infinite sequence of Excel column names. This makes solving the problem rather easy.

Here’s the one-liner:

raku -e 'say { $^a ~~ Str ?? .antipairs.grep(*.key eq $^a)[0].value + 1 !! $_[$^a - 1] given "A"..* }($_) for (28, "AD")'

And here’s the output:


Task #2

Write a script that accepts list of positive numbers (@L) and two positive numbers $X and $Y.

The script should print all possible numbers made by concatenating the numbers from @L, whose length is exactly $X but value is less than $Y.

Assuming we know @L, $X and $Y, this is my solution:

raku -e 'say ($X > 1 ?? [X~] @L xx $X !! @L).grep(* !~~ /^0.+/).grep(* < $Y)'

Note that [X~] @L xx 1 is just [X~] @L, which we avoid by checking $X > 1.