Perl Weekly Challenge 012
This week's challenge had a great problem selection. Both parts had succinct easy to understand problem statements and yet they both allowed lent themselves easily to the variety of solutions styles of the sort Perl is famous for. 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.
$ perl perl5/ch-1.pl
E_6 = 30031 is composite.
For Part 1 I decided to not bother computing the primes myself. After deciding that I further thought that having a table of hard coded primes in the code was a bit boring and so I found a web site that had the table of many primes already computed. My code starts by grabbing that table of pre-computed primes (the first 10k) with a basic LWP::UserAgent and feeding them into an array. The page that is sent includes some header text but the primes themselves are arranged in rows of 10. To make sure we only include those numbers in our array we check to make sure that the split rows of input that have less than 10 columns are ignored (lines 21-23).
The rest of the solution proceeds pretty much as you'd expect with increasing sized array slices of the array of primes being used to compute successive Euclid Numbers and then checking to see if they are prime. One somewhat uncommon technique is the use of eval to compute the product of primes on line 29.
The first composite Euclid Number is printed if found and then the code immediately exits. If not, a message informing that none are detected is shown.
$ perl perl5/ch-2.pl
Path in common is /a/b.
Part 2 had some potential for an elaboratly over engineered solution if I felt so inclined. I think this might be a fun way to invlove Graphs, for example, as it was reminiscent of the previous Word Ladder challenge. I did not have the ability to dive into a deliberately over engineered solution this week and so stuck to basics!
The file separator is expected to be the first line of input. That is read in and from here the rest of the input is assumed to be paths to check. Each path is stored as an array reference containing the directories. This is done by splitting on the file separator (line 17). Note the use of quotemeta in order to make sure that the file separator is properly escaped during variable interpolation.
Paths are checked by removing the first directory from the first path and then comparing to the first directory in all the other paths. If they all match then that directory is saved and successive directories are checked until no directories remain or a non-match is detected.
- chop is not a very frequently used function but it is convenient for the sort of thing used for here (line 38) which is to remove the trailing file separator from the output.
- The last on line 35 is to make sure that we exit the outermost loop and do not continue to check directories.
- This code (intentionally!) has a particular style to it that I would describe as an older C style of coding because of the way I wrote the for loops, the boolean flag $in_common, and the use of last. I think it's very readable and easy to follow, although it's different from what would be called idiomatic Perl.
- A more general thought on code style: for a very long time now, at least twenty years, you really only hear about programming style when discussed in a negative context. For example, a company may have a specific style guide which enforces rules of how code should be written. Even more strictly, these style guides are many times enforced with code checking tools that examine code as it is checked into source code versioning repositories. This is somewhat depressing because before this era of style enforcement there was a view that code could be written in a way that could be described as beautiful in the same way that a work of fine art may be considered. Knuth's Literate Programming book comes to mind as an example of this school of though, in particular the very first chapter. I have yet to find a style guide for any programming language which yields code I find particularly attractive either in an aesthetic sense, or in any sense of elegance of implementation. The Perl programming community may very well be the last active area of Computer Science where code creativity and style has any appreciation.