Perl Weekly Challenge 004

Another week, another chance to get creative with some Perl!

Part 1

This code is done to solve the stated problem as generally as possible! That means that instead of golf-ing the code down to such a small size that I could have just used sprintf("%1.Xf", ...) where X is the size of the, presumably, one-liner I instead put the code in a file and use stat() to get the size of that file. The digits of π  are then computed as needed using a so-called spigot algorithm. This name comes form the fact that the algorithm delivers an endless supply of digits, like water flowing from a spigot.

This computation of the digits of π uses continued fractions. The method, the mathematics behind the algorithm, and some Ruby code implementing the method (which I drew heavily from!) can be found here.

For the record the one-liner solution would look like this:

    perl -e 'print(sprintf("%1.37f",4*atan2(1,1)))'

Part 2

This code is inspired greatly by the solution to Challenge 003's first part. This time instead of creating a few individual closures I create an array of them, one for each of the given letters. These are then used in a recursive loop to remove letters from each word being examined. If we use up all the closures (letters) and the word still i snot empty then the word does not pass. If the word is reduced to the empty string (whether we have closures left or not) indicates the word passes.

Note: I did not follow the exact wording of the challenge in that the letters and words are not coming from separate files. Changing the code to get the words and letters from a file would not change the logic of the main parts of the code at all, however. 

Perl Weekly Challenge 003

Both parts of this week's challenge seemed to have the same theme of performing a calculation that inspires recursion. I wrote a bit about recursion in Perl last week so there isn't too much new to add on that subject in describing the code.

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

This a pretty straightforward solution to the stated problem. To jazz things up a little I used currying to generate the three is_divisible_by_X functions. This is neat to see in practice, but is never required. In fact, I've never seen currying used seriously in Perl. Just these sorts of toy problems and examples. If anyone is interested in why someone would want to do this I refer you to a nice Perlmonks post on why currying may have some practical usage in other programming environments.

I generally avoid using any CPAN modules for these problem sets but I made a small exception in the code for Part 1 and use boolean; this module simply allows the use of true and false as boolean values. Completely unnecessary, of course, but I have always preferred the aesthetics of boolean keywords.

Part 2

The computation for Pascal's Triangle (binomial co-efficients) is, similar to the Hamming Numbers of Part 1, straightforward. This week perhaps my creativite energy was a bit low. The solution for Part 2 is about as vanilla as you can get!


Perl Weekly Challenge 002

Below is my take on each of the two parts to challenge 002. 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

Collapse )

Perl Weekly Challenge 001

I just learned about https://perlweeklychallenge.org/ which posts a small coding challenge each week. They seem to have started up 25 March 2019 so, as of this writing, there have been two challenges posted. Below is my take on each of the two parts to challenge 001. 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

Collapse )