RabbitFarm

2019-05-05

Perl Weekly Challenge 006

Perl is an extremely expressive language which gives a tremendous amount of flexibility to the programmer. Combined with a deep module ecosystem (i.e. CPAN) a small amount of Perl code can be extremely powerful!

This week I was not feeling well and allowed myself some shortcuts. Rather than take the time to implement necessary routines myself in some interesting (to me) way I instead opted to make use of a couple of modules and be done with it. All the sooner to get back to feeling sick and tired.

As with the previous weekly challenges the problem statements are short and are included in the first comment of the code. The code blocks shown link to GitHub Gists.

Part 1

Data::Dump is one of my favorite debugging tools. The pp() function "pretty prints" the contents of a data structure and, in doing so, performs the sort of compaction required in the first part of this week's challenge. All that is necessary is to call pp() and then do a bit of polishing on the final output by removing unnecessary parentheses and white space.

Sample Run

$ perl ch-1.pl 1,2,3,4,9,10,14,15,16

1-4,9,10,14,15,16

Ok, maybe it's somewhat off the requirements since pp() wants to compact 4 or more continuous numbers and not 3 or more. Did I mention I was feeling sick? Close enough!

Part 2

I had dreams of writing my own arbitrary precision code for this. Fever dreams that is! Math::BigFloat helped make quick work of this.

Sample Run

$ perl ch-2.pl

262537412640768743.9999999999992500725947

posted at: 20:56 by: Adam Russell | path: /perl | permanent link to this entry