## Kian-Meng Ang Weekly Review: Challenge - 011

Thursday, Jun 13, 2019| Tags: Perl

Continues from previous week.

For a quick overview, read through all questions and recap of the challenge first.

For those who are new to the weekly review post, the main purpose of weekly review is to document down interesting approaches of using Perl programming language to solve each week challenges. Code reading or review allows us to learn / relearn, discover / rediscover, and appreciate / reappreciate (some participants uses other programming languages in their day job) new, interesting, and quirky things accomplished with Perl (unfortunately Perl 5 for now). At the end of the day, everyone have fun, shared their knowledge, and learned something along the way.

## Challenge #1

Most of the submitted solutions were well documented or written in a self-documented way thus making code reading easier and more approachable. Most of the solutions were using direct algebra calculation, Bisection method, and intersection between two lines or points.

Andrezgz caught our attention, as what he commented, “simple, but effective” way of using while loop without additional variable to store the guard condition.

``````use strict;
use warnings;

my \$f = 212;
while ( \$f - (\$f - 32) * (100-0)/(212-32) ) { \$f-- }; #Simple, but effective
print \$f;
``````

Notable solution by Gustavo Chaves where he used the same module, `Math::MatrixReal` to solve both Challenge #1 and #2. As they said, “kill two birds with one stone”. He also submitted an unofficial shortest answer to this challenge or to any challenges ever.

``````#!/usr/bin/env perl
print "40\n";
``````

Jokes aside, it’s good that some participants were having fun and writing witty comments in their solutions. You know who you are.

## Challenge #2

This is a very straight forward question and simple question will always lead to creative and unique solutions. There are several approaches which includes simple loop, loop through `map` function or repeating (*) characters, or using CPAN modules like `Math::Matrix`, `Math::MatrixReal``PDL`, or `PDL::MatrixOps`. Laurent Rosenfeld demonstrated this through different iterations of his solutions. Read his blog post for walk through of each iteration. Again, Andrezgz’s solution caught our attention, while it’s not really an array of matrix, it’s just a creative way to print out an Identity Matrix.

``````my \$n = int(\$ARGV[0]) - 1;
for (0..\$n) { print " 0" x \$_ . " 1" . " 0" x (\$n - \$_) . \$/; }
``````

As usual, shortest answer went to E. Choroba, even though he used a CPAN module.

``````#!/usr/bin/perl
use warnings;
use strict;

use PDL;
use PDL::MatrixOps;

print identity(shift);
``````

## Challenge #3

There were five participants (Dave Jacoby, Trevor Vaughan (athanasius), Feng Chang, Steve Wilson, and Joelle Maslak) who submitted their answer to challenge #3 for this week. Hopefully we can see even more in coming weeks. One thing you will notice during code reading is that some participants don’t mind using the latest and non-default Perl’s features through `feature` pragma. Or also known as “I know what I’m doing”. From Dave Jacoby.

``````use feature qw{ postderef say signatures state switch fc };
no warnings qw{ experimental::postderef experimental::smartmatch experimental::signatures };
``````

Or from Joelle Maslak.

``````use feature 'signatures';
no warnings 'experimental::signatures';
``````

Or non-core feature like `Switch` module as coded by Steve Wilson, which I believed was used to substitute the missing feature of `switch` in core Perl.

``````switch ( \$weather{"weather"}[0]{id} ) {
case [ 200 .. 232 ] { \$condition = "thundery"; \$item = "golf club" }
case [ 300 .. 321 ] { \$condition = "drizzly";  \$item = "lemon cake" }
case [ 500 .. 531 ] { \$condition = "rainy";    \$item = "wellies" }
case [ 600 .. 622 ] { \$condition = "snowy";    \$item = "sledge" }
case [ 701 .. 781 ] {
\$condition = "low visability";
\$item      = "flashlight"
}
case [800] { \$condition = "clear skies"; \$item = "sunscreen" }
case [ 801 .. 804 ] {
\$condition = "cloudy";
\$item      = "imagination, what shapes can you see?"
}
}
``````

What are the common HTTP client modules used among the participant? The module name with count in parenthesis are `Mojo::UserAgent` (2), `LWP::UserAgent` (1), `WWW::Mechanize` (1), and `LWP::Simple` (1). Neil Bowers have made a comprehensive comparison of CPAN modules for making HTTP requests and yet nobody is using `HTTP::Tiny`? One issue Perl programmer faced is the enormous amount of CPAN modules and it’s difficulty to choose a suitable and recommended module.

For the amount of codes and features added by Athanasius, perhaps it would be suitable to turn it into a full blown CPAN module like `App::OpenWeatherMap`?

## Blog Posts

Additional blog posts by some of the participants for more intricate details.

(1) FC Matrix with Perl 6 by Arne Sommer. If you want to read line-by-line explanation of Perl 6 solutions to these challenges, his post is a must read.

(2) Perl Weekly Challenge 011 by Adam Russell. Regular post on challenge.

(3) Two Technical Posts about Temperature, Kinda by Dave Jacoby. Look into his write-up on the third challenge regarding API. He took another approach compare to Joelle Maslak.

(4) Perl Weekly Challenge # 11: Fahrenheit Celsius and Identity Matrix by Laurent Rosenfeld. If you need a comparison of Perl 11 (Perl 5 and Perl 6) solutions, then his blog is a good place to start.

(5) Perl 6 Small Stuff #20: From anonymous one-line functions to full length MAINs with type and error checking by Jo Christian Oterhals. Another discussion on using Perl 6.

(6) Perl Weekly Challenge 11: Temperature Scales And Identity Matrices by Yozen Hernandez. Write-up on the mathematical solution and different approaches.

(7) Perl Weekly Challenge 011: Fahrenheit, Celsius, and an Identity Matrix by E. Choroba. For introduced `PDL` module to us when working with matrix.

## SO WHAT DO YOU THINK ?

If you have any suggestions or ideas then please do share with us.