adamcrussell

Perl Weekly Challenge 013

These weekly challenges always seem to open the door to further reading and research into interesting topics and this week was no different. Part 1 this week opened the door to the surprisingly deep body of work on algorithms to determine the day of the week. Most modern implementations trace themselves only as far back to 1992 when Tomohiko Sakamoto posted his algorithm to the UseNet group comp.lang.c. Part 2 led to some interesting reading on mutually recursive methods.

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
2019/01/25
2019/02/22
2019/03/29
2019/04/26
2019/05/31
2019/06/28
2019/07/26
2019/08/30
2019/09/27
2019/10/25
2019/11/29
2019/12/27

I stuck with what seems to be the more or less usual implementation of the Sakamoto algorithm. In an effort to hopefully make the code a bit more readable I made liberal use of use constant. This is something I like to do and it often comes up in my code. I think the effects are generally positive, the extra verbosity never seems to hurt. In general I like to cut down on as many literal strings, ints, and floats whenever possible. These used to be referred to as "magic numbers" because if you saw them in someone else's code without explanation their meaning might be hard to explain ... simply magic! 

In reading up on this topic I found a nice web page that, in addition to the usual discussion, also went through the steps to easily do this computation in your head, provided you memorize a few key details.

Part 2 

Sample Run

$ perl perl5/ch-2.pl
0 M: 0 F: 1
1 M: 0 F: 1
2 M: 1 F: 2
3 M: 2 F: 2
4 M: 2 F: 3
5 M: 3 F: 3
6 M: 4 F: 4
7 M: 4 F: 5
8 M: 5 F: 5
9 M: 6 F: 6
10 M: 6 F: 6
11 M: 7 F: 7
12 M: 7 F: 8
13 M: 8 F: 8
14 M: 9 F: 9
15 M: 9 F: 9
16 M: 10 F: 10
17 M: 11 F: 11
18 M: 11 F: 11
19 M: 12 F: 12
20 M: 12 F: 13
21 M: 13 F: 13
22 M: 14 F: 14
23 M: 14 F: 14
24 M: 15 F: 15
25 M: 16 F: 16

This part of the challenge was about as straightforward as you can get I think. Even the MathWorld entry on the subject is pretty lean. If you read up on mutual recursion you might find it occurs commonly in parsers which are based on grammars which have recursive production rules. In a previous posting I wrote about a simple grammar for roman numerals. The use of grammars for that problem was intentionally over engineered as an excuse to explore grammars and parsers more and Part 2 this week reminded me that I had wanted to spend some more time on that ...

Comments for this post were locked by the author