Kian-Meng Ang Weekly Review: Challenge - 022

Sunday, Sep 1, 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.


Task #1


CPAN modules used: Const::Fast, Function::Parameters, LWP::UserAgent, List::Util, Math::Primality, Math::Prime::Util, Memoize, Modern::Perl, autodie, boolean, constant, feature, lib, strict, utf8, warnings

We have yet another question related to prime number, therefore most participants will reuse back their solution from previous challenge. For this time, we need to generate a series of sexy primes.

Two common approaches used by particpants to generate the series either through addition or subtraction of difference of 6 between two prime numbers.

Participants whose solution belongs to the former group were Duane Powell, Athanasius, Daniel Mantovani, Laurent Rosenfeld, Jaldhar H Vyas, Roger Bell West, Guillermo Ramos, Kian Meng Ang, Randy Lauen

For the later group, see the solution by Adam Russell, Ruben Westerberg, Yet Ebreo, Duncan C White, Steven Wilson, Andrezgz, E. Choroba, and Joelle Maslak

One quick way we learned about checking for primality as discovered by Daniel Mantovani was to use regex asshown below.

sub is_prime {
    return ( 7 x shift ) !~ /^(77+)\1+$/;
}

For those who use Math::Prime::Util, CPAN module, Randy Lauen demonstrated a better way to use the CPAN correctly and effectively through built-in iterator function. Similar but without use of any CPAN modules, see E Choroba‘s solution.

Yet Ebreo decided to upheld the Perl way of writing obfuscated code by shorten his code into two lines.


Task #2


CPAN modules used: Const::Fast, Data::Dumper, English, Getopt::Long, List::Util, Modern::Perl, Moose, Test, Test::More, autodie, bignum, bytes, constant, feature, lib, strict, warnings

For those who don’t know how LZW compression works, you can start with Duane Powell‘s solution. It’s well commented and structured. If you prefer more concise but still readable code, look into E. Choroba‘s solution. Laurent Rosenfeld wrote a good post on this task as well.

Again, Randy Lauen surprised us with the extra effort of writing test case to verify his solution in a nicely formatted layout.

$ wget http://corpus.canterbury.ac.nz/resources/cantrbry.tar.gz
$ tar zxvf cantrbry.tar.gz
$ perl test-lzw.pl .
+-----------------+---------+------------+--------+
| File            | Size    | Compressed | Ratio  |
+-----------------+---------+------------+--------+
| alice29.txt     | 152089  | 70226      | 46.2%  |
| asyoulik.txt    | 125179  | 62748      | 50.1%  |
| cantrbry.tar.gz | 739071  | 1068298    | 144.5% |
| ch-1.pl         | 332     | 442        | 133.1% |
| ch-2.pl         | 2136    | 1946       | 91.1%  |
| cp.html         | 24603   | 14948      | 60.8%  |
| fields.c        | 11150   | 7084       | 63.5%  |
| grammar.lsp     | 3721    | 2818       | 75.7%  |
| kennedy.xls     | 1029744 | 365572     | 35.5%  |
| lcet10.txt      | 426754  | 184752     | 43.3%  |
| plrabn12.txt    | 481861  | 218914     | 45.4%  |
| ptt5            | 513216  | 70242      | 13.7%  |
| sum             | 38240   | 25054      | 65.5%  |
| test-lzw.pl     | 2169    | 2120       | 97.7%  |
| xargs.1         | 4227    | 3584       | 84.8%  |
+-----------------+---------+------------+--------+

Joelle Maslak submitted probably the most comprehensive solution for this task at bit-level (likewise for Guillermo Ramos).


Task #3


CPAN modules used: None

There was none submission for this challenge. Again, for those who haven’t try this task before, do give it a shot.


SEE ALSO


(1) Sexy Primes, LZW and Perl 6 by Arne Sommer

(2) Perl Weekly Challenge - 022, Task #1 by Mark Senn

(3) Perl Weekly Challenge 022 by Adam Russell

(4) Perl Weekly Challenge # 22: Sexy Prime Pairs and Compression Algorithm by Laurent Rosenfeld

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

(6) RogerBW’s Blog: Perl Weekly Challenge 22 by Roger Bell West

(7) Perl Weekly Challenge W022 - Sexy Primes by Yet Ebreo

SO WHAT DO YOU THINK ?

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

Contact with me