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”