I thought it should be easy to write a solution in Python, so I started with the following code:
from datetime import timedelta
start = datetime.date(2009, 1, 1)
end = datetime.date(2013, 1, 1)
while start < end:
print start.strftime('%Y-%m-%d %A')
start = start + timedelta(1)
This little program prints out all the dates from 2009-01-01 to 2012-12-31 in the following format:
I could then have added some more code to determine if a date was a Friday 13th and printed it - but decided not to. I had a general purpose tool that printed a calendar, so all I needed to do was the following:
python printcal | grep '13 Friday'
Which then gives the following:
So, why did I prefer to do this rather than modify my Python script. Let's look at the Unix Philosophy
This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.
A second part of the philosophy from Mike Gancarz is "Look for the 90-percent solution."
So, I have created a small tool that prints all the dates in a period (this is the 90-percent solution). It is easy to verify that it is doing what I want to do. I use the output of this program as the input to the standard unix program
grepthat finds all the output that contains Friday 13. Suppose I now want to find all years that have at 3 months where the first of the month is a Monday, I can do it easily by modifying the
greppart of the pipeline. If I want to see how many Monday 5ths we had in 2008, I can pipe the output of
wc -lwhich will print the number of lines output.