Perl Weekly Challenge 011

Both parts of this week challenge seemed straightforward. Details below!

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

Sample Run

$ perl perl5/ch-1.pl
°F = °C at -40

Given the formulas for converting between the two scales you could really do this problem via hand calculations. Still, this is the Perl Weekly Challenge and so writing some Perl code seems to be in order! The code iterates over values for °C until it finds the equal point.

Part 2

Sample Run

$ perl perl5/ch-2.pl
10 x 10 identity matrix:
   1 0 0 0 0 0 0 0 0 0
   0 1 0 0 0 0 0 0 0 0
   0 0 1 0 0 0 0 0 0 0
   0 0 0 1 0 0 0 0 0 0
   0 0 0 0 1 0 0 0 0 0
   0 0 0 0 0 1 0 0 0 0
   0 0 0 0 0 0 1 0 0 0
   0 0 0 0 0 0 0 1 0 0
   0 0 0 0 0 0 0 0 1 0
   0 0 0 0 0 0 0 0 0 1

This is a case where using CPAN would have reduced the problem to a single line of code. The PDL extension package has an identity function, after all. In the spirit of gamesmanship I wrote the above code which certainly does the job and if you really wanted to use it in an actual matrix calculation could be converted to the appropriate object using the pdl constructor.

Summary

This week's challenges seemed straightforward to implement in an ordinary way. I suppose this lends itself to some prime code golfing. I passed on the chance this time around!

Comments for this post were locked by the author