Kian-Meng Ang Weekly Review: Challenge - 026

Sunday, Sep 29, 2019| Tags: Perl5

Continues from previous week.

Feel free to submit a merge request or open a ticket if you found any issues with this post. We highly appreciate and welcome your feedback.

For a quick overview, go through the original tasks and recap of the weekly challenge.


Task #1


CPAN modules used: Const::Fast, Getopt::Long, List::Util, Test::More, feature, strict, warnings

As with past challenges, simple task will lead to diverse solution. Sometimes straight forward task is harder if we want to do thing differently.

Let’s start with participants with simple and naive approach. No one-liner or fancy approach. Lubos Kolouch took such approach and it’s a good solution to use as baseline for our comparison.

Duane Powell took used regex substitution and count approach to solve this task. Similarly for Daniel Mantovani but with a shorter one-liner version and E. Choroba with test cases.

Coincidentally, the List::Util CPAN module was used by both Ruben Westerberg and E. Choroba to filter out unique characters. However, while the module was added to the later solution, it was not used.

Next, the split-grep-map approach was a common approach to solve this task. Participant who took such approach were Athanasius, Laurent Rosenfeld, Roger Bell West, Duncan C White, Steven Wilson, Colin Crain, and Joelle Maslak.

On a related note, probably the most fluid comment we’ve read in a challenge by Andrezgz.

print scalar                      # print the number
    grep {
        $_ =~ /[A-Za-z]/          # of alphabethic case insensitive characters
        && index($w2, $_) != -1   # that exist on the second word
    } split //, $w1;              # from each one of the first wor

And kudos to participants (Lubos Kolouch and E. Choroba) who included test cases in their solutions. Good to see some edge cases were tested as well. However, there was no test case to verify case sensitivity of the comparison.

Both Adam Russell and Yet Ebreo solutions were the example of polar opposite answer we’ve seen in a challenge. The former loves over-engineered a solution and the later preferred to shorten it as much as possible into one-liner. Either way, we welcome the diverse coding styles and approaches as everyone can learn something from it. And this is Perl, TMTOWTDI.


Task #2


CPAN modules used: Const::Fast, Data::Types, Getopt::Long, List::Util, Math::Trig, Test::More, constant, feature, strict, utf8, warnings

We can generally divided the submitted solutions into two groups, those who used Math::Trig CPAN module to do the degree to radian or vice versa conversion and those who don’t.

For this task, start with the solution by Daniel Mantovani and Joelle Maslak. The former have good written comment to let you have an overview of the problem and solution and the later served as a good comparison. Both solutions were just a top to bottom plain script with no subroutine. This was followed by solution Roger Bell West which shared similar coding style but at a very condense code.

Next, move to the solution by Jaldhar H. Vyas and Adam Russell where we start to see abstraction being applied here. Subroutines on conversion between radiand and degree were observed in their solutions.

Even further refinement, look no further at the solution by Laurent Rosenfeld, which was simple but comprehensive with full test coverage (likewise for Steven Wilson). Usually, we would recommend this submission as the first solution to read but for demonstrating the possible refactoring process flow, this should come later. Alternatively, the solution by E. Choroba illustrated the possible way to refine the code.

Some interesting stuff that caught our attention.

This was the first time we notice that organiser MANWAR’s CPAN module, Data::Types was used by a participant as observed in the solution by Athanasius. Good that some participants tried to use different CPAN modules to solve the task.

Sometimes it’s nice to the explicit about what subroutines being imported when using a CPAN module as observed in the code by Andrezgz.

use Math::Trig qw/rad2deg deg2rad/;

SEE ALSO


(1) Perl Weekly Challenge 026 by Adam Russell

(2) String Angling with Perl 6 by Arne Sommer

(3) Stones and Jewels by Donald Hunter

(4) Perl Weekly Challenge 026: Stones and Jewels; Mean of Angles by E. Choroba

(5) Perl Weekly Challenge: Week 26 by Jaldhar H. Vyas

(6) Perl Weekly Challenge 26: Common Letters and Mean Angles by Laurent Rosenfeld

(7) Perl Weekly Challenge 26 by Roger Bell_West

SO WHAT DO YOU THINK ?

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

Contact with me