PWC 048 › Palindrome Dates (mm/dd/yyyy)

This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)

Task #2 this week asks us to (and I quote): print all Palindrome Dates between 2000 and 2999. The format of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is represented as 10022001.


It’s pretty easy to avoid using any sort of date library with a couple of key observations about the problem domain. First, we’ll split it up into month (mm), day (dd), century (cc), and 2-digit year (yy). Thus our “baseline” date format is mm dd cc yy.

If we use R[xx] as shorthand for “string reverse of xx“, we can rewrite the date in a couple of different ways:

  mm    dd    cc    yy     # Original
R[yy] R[cc]   cc    yy     # Start with year
  mm    dd  R[dd] R[mm]    # Start with mm/dd
R[yy]   dd  R[dd]   yy     # Start with yy and dd
Continue reading “PWC 048 › Palindrome Dates (mm/dd/yyyy)”

PWC 047 › Roman Calculator

This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)

The first challenge this week tasks us with evaluating arithmetic operations using Roman numerals. The description seems to indicate that only one operation will be given (for example, II + IV), but I have elected to support arbitrary arithmetic expressions.

First, I needed a way to convert to and from Roman and Arabic numerals. I had coded this up years ago, and adapted the code I used. Here is the Roman to Arabic converter:

Continue reading “PWC 047 › Roman Calculator”

PWC 047 › Gapful Numbers

This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)

Task 2 this week has us print the first 20 “gapful numbers,” as described by OEIS sequence A108343. Gapful numbers are numbers greater than 99 that are evenly divisible by their first and last digits combined. For example, 132 is a gapful number because 132 ÷ 12 = 11.

This is certainly the easier of the two tasks, as both Perl and Raku have convenient ways to index and concatenate string fragments.

Continue reading “PWC 047 › Gapful Numbers”

PWC 046 › Cryptic Message

Can you hear me now? Good.

This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)

Challenge #1 this week is the following:

The communication system of an office is broken and message received are not completely reliable. To send message Hello, it ended up sending these following:

H x l 4 !
c e - l o
z e 6 l g
H W l v R
q 9 m # o
Continue reading “PWC 046 › Cryptic Message”

PWC 046 › Is the Room Open? (500 Doors)

Partial result of flipping every 2nd, then every 3rd, and so on

This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)

The second of the challenges this week poses the following question (paraphrased):

Suppose we have 500 doors, and 500 employees. The first employee opens all the doors. The second employee closes every 2nd door (doors 2, 4, 6, … 500). The third employee closes every third door if it is open, or opens it if closed. And so on. Which doors are open after all 500 employees have been through?

I remembered toying with a very similar problem a few years ago. At the time, I actually wrote a terminal-based visualizer for it, which you can see in action below (the animated GIF is 1.7MiB, so it may take a few seconds to load on slower connections):

Continue reading “PWC 046 › Is the Room Open? (500 Doors)”

Quine

This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)

Challenge #2 this week asks for “a script that dumps its own source code”. This is almost a quine, although Mohammad did not name it as such. Specifically, we are given the constraint that perl ch-2.pl | diff - ch-2.pl must return nothing.

What’s a Quine?

A quine, otherwise known as a self-replicating program, is a program that accepts no input, and produces a copy of its source code as its only output.

This is a stronger definition than what Mohammad has asked for this week: specifically, Mohammad did not add any restrictions on input. So, programs that simply read their own source file would be acceptable this week. Since I felt that was too easy, I have decided to go with the stronger definition of an actual quine, not allowing any input. Still, I’ll include a few solutions to show the various options:

Continue reading “Quine”

Leonardo Numbers

This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)

Happy new year! We are on Week 41, and this is Challenge #2.

The Leonardo Numbers (A001595) are a simple recursively defined sequence:

\(
L(n) = \begin{cases}
1 & \text{if } n \lt 2 \\
1 + L(n – 1) + L(n – 2) & \text{if } n \geq 2
\end{cases}\)

The sequence starts: 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, …

You’ll note this sequence is very similar to the well-known Fibonacci sequence, which differs only in that the Fibonacci sequence does not have the + 1 term, and starts at F(0) = 0.

Continue reading “Leonardo Numbers”

Attractive Numbers

This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)

This week, Mohammad asks us to output a list of all Attractive Numbers between 1 and 50. Attractive numbers, as described by the Online Encyclopedia of Integer Sequences (OEIS) are:

Numbers with a prime number of prime divisors (counted with multiplicity)

OEIS Sequence A063989

The first numbers are 4, 6, 8, 9, 10, 12, 14, 15, 18, 20, 21, 22, 25, 26, 27, 28, 30, 32, 33, 34, 35, 38, 39, 42, 44, 45, 46, 48, 49, 50, …

This is a straightforward problem, but I’m not going to let that stop me from finding an interesting way to solve it. If I were looking for a sensible way to solve it, I’d do something like this:

use Math::Prime::Util ':all';
say for grep { is_prime( factor($_) ) } 1..50;
Continue reading “Attractive Numbers”

Zip6

This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)

The zip6 function has long been a staple of the List::MoreUtils CPAN module. The Week 40 challenge #1 describes it very well. But even more succinctly: zip6 takes an array of arrays (AoA) and returns another AoA of all the 1st elements, then the 2nd, and so on.

It’s not a difficult algorithm by any stretch. In fact, the description more or less tells you how to do it. Simply iterate through all of the arrays in parallel, building up the resulting array as you go.

Continue reading “Zip6”

Reverse Polish Notation

This is my first blog post regarding the Perl Weekly Challenge tirelessly maintained by Mohammad Anwar. Although I’ve been doing the challenge for a while now, I haven’t maintained my blog for years. Let’s change that!

This week’s challenge #2 from the Perl Weekly Challenge is a computer science classic: create a Reverse Polish Notation (RPN) calculator. This brings back fond memories.

Basic Algorithm

My Perl 5 and Raku solutions will both use the same basic algorithm. First, we tokenize the input into numbers and operators. Then, we loop through every token. For each token, we either:

  1. If the token is a number, we add it to the stack
  2. Otherwise, it’s an operator:
    1. Pull zero or more items off the stack depending on the operator
    2. Perform the required operation (e.g., addition, subtraction, etc.)
    3. Push the result onto the stack
Continue reading “Reverse Polish Notation”