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. 


posted at: 17:03 by: Adam Russell | path: /perl | permanent link to this entry