Kian-Meng Ang Weekly Review: Challenge - 019

Saturday, Aug 10, 2019| Tags: Perl

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.

This week challenge was quite straight forward and they said, simpler tasks will always leads to more creative solutions.


Task #1


In general, this was quite a straight forward question. Participant can either find the month in a naive and brute force way or selectively through certain criteria. These criteria includes a month which start on Friday and have 31 days.

Some submitted solutions we noticed that were doing things differently.

Out of many ways to display the result, Jaime was definitely thinking out of the box. The solution used the result from the console calendar program, cal to show the month with 5 weekends. This was definitely a nifty approach to visualize the output. If you can’t get the script to run, remove the -h parameter.

This was one of the trend we noticed in last few challenges. Participants do not use or minimize the use of any external CPAN modules in their submitted solutions. Why? As Daniel Mantovani puts it, “it will probably not be that challenging to do so”. Hence, we noticed participant like Adam Russell and Jaldhar H. Vyas who took this approach by writing additional codes to check for day of the week or detecting leap year instead of relying on external CPAN module.

Nevertheless, some participants still depends on CPAN modules for a quick solution. Common date-related CPAN modules used for this tasks includes Time::Piece, Time::Local, Date::Time, Date::Manip, Date::Calc, Time::Seconds, and POSIX.

If you saw a sequence of number, 1, 3, 5, 7, 8, 10, 12 or 0, 2, 4, 6, 7, 9, 11 used to exclude certain months and wonder why? Look at the solution by Dave Jacoby, Gustavo Chaves, Athanasius, and Daniel Mantovani. They have put in the effort to write good comment on this magic sequence. Basically, these numbers are 31 day month (Daniel Mantovani have a good explanation on why you need 31 days), which is one of the criteria for a month to have 5 weekends.

And if you noticed from these solutions, some participants love to write comment using POD markup or file literals, the content after the __END__ token. The main difference is POD markup will fail if there were syntax errors where all text after the __END__ token will be ignored by Perl compiler. We’re not sure where and how Prajith P uses Perl for, but he seemed to install it in an unconventional path by the looks of the shebang line.

    #!/opt/deployer/embedded/bin/perl -w

As usual, participants who used different ways to loop through the years and months. Andrezgz was using do...until, E. Choroba preferred while...continue, and Joelle Maslak picked map and grep instead.


Task #2


Two steps needed to solve this problem. First was to split the paragraph into individual token or word. And second, rearrange or wrap the text according to the width length.

Compare to Task #1, this was more harder if you want to solve it differently instead of following the pseudocode of the greedy algorithm. Did any participants solved it differently? Yes, a few of them.

Athanasius was the only participant where the solution can wrap text with optionally indentation and multiple paragraphs.

Both Laurent Rosenfeld and E. Choroba used an alternative way which split a line rather than words to wrap the text by using rindex function. Gabor Szabo wrote about this approach in details.

For CPAN module consumers, Peter Houston used Text::Wrap to solve this problem. For Jamie, he used Text::ParseWords to split and extract the words from the standard input.

While this approach was seldom used, E.Choroba used here doc in the test suite was something new we’ve seen so far. Do you know that indentation was supported since Perl v5.26?

    << '__PAR__', << '__EXPECTED__' =~ s/\n$//r);
ABCDE
AABBCC
A BB CC
AA BB CC
AAA BB CC
AAAA BB CC
AAAAA BB CC
__PAR__
ABCDE
AABBC
C A
BB CC
AA BB
CC
AAA
BB CC
AAAA
BB CC
AAAAA
BB CC
__EXPECTED__

Task #3


The regular participants were back again. For Perl 5, Steven Wilson and Joelle Maslak and for Perl 6, Randy Lauen and Joelle Maslak again.

For those who haven’t done this task before, do give it a try, especially those using Perl 5 where you can pick different HTTP client CPAN modules.


Blog Posts


Some write-ups from the usual suspects.

(1) Word Wrapped Weekends, Perl 6 Edition by Arne Sommer

(2) Perl Weekly Challenge 019 by Adam Russell

(3) Five-Weekend Mondays and How Years Work by Dave Jacoby

(4) Counting Weekends And Wrapping Text by Dave Cross

(5) Perl Weekly Challenge # 19: Weekends and Wrapping Lines by Laurent Rosenfeld

(6) Perl 6 small stuff #21: it’s a date! …or: learn from an overly complex solution to a simple task by Jo Christian Oterhals

(7) Perl Weekly Challenge: Week 19 by Jaldhar H. Vyas

(8) Perl Weekly Challenge 19 by Roger Bell West

(9) Greedy expression of the best months – Perl weekly challenge 19 by Francis Whittle

(10) Perl Weekly Challenge 019: Five Weekends and Paragraph Wrapping by E. Choroba

SO WHAT DO YOU THINK ?

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

Contact with me