Greetings fellow monks :)

I lead a small team of people that work a 24/7 shift roster. One of my less exciting duties is to maintain the roster and provide weekly updates. Whilst I was preparing this weeks update (which I do each Friday), I noticed something interesting which eventually led to quite a fascinating (for me, at least) discovery.

Each week, as I update the roster I save a copy, giving it a filename indicating the day/date. This week, as I was saving the file I noticed that in 2007, Friday has occurred on every day of the month. ie. (1 .. 31).

So this got me to wondering...

  1. Was this true of every day of the week?
  2. How does this vary from year to year, and is there any pattern involved?
So naturally, I wrote some code to find out :)

The result I got was quite surprising, and at first I thought there must be a silly bug in my code that was giving an erroneous result. But I've done some manual (Eyeball MK-II) checking, which does appear to verify it. Although, I am still half-expecting somebody to point out an error and make me look _really_ silly :)
In early iterations of the code I wrote, I was just testing a single year at a time. But as I kept getting similar results no matter which year I gave it, I eventually decided to test all years from 1 .. 2500.

The code I used is given below, but before I get to that - a couple of things to ponder....

  1. Without running the code or writing your own, can you predict the result?
  2. How would you have written this code? (golfers, please step forward)
#!/usr/bin/perl -l use strict; use warnings; use Date::Calc qw/Days_in_Month Day_of_Week/; my @days_of_week = qw/Mon Tue Wed Thu Fri Sat Sun/; for my $year (1 .. 2500) { my %days_of_month; for my $month_of_year(1 .. 12) { for my $day_of_month(1 .. Days_in_Month($year,$month_of_year)) + { my $dow = Day_of_Week($year,$month_of_year,$day_of_month); # Day_of_Week uses a 1-based index, hence the --$dow $days_of_month{$day_of_month}{$days_of_week[--$dow]}++; } } for my $day_of_month (sort {$a <=> $b} keys %days_of_month) { next if scalar keys %{$days_of_month{$day_of_month}} == 7; my @missing_days; for (@days_of_week) { push (@missing_days, $_) if !defined $days_of_month{$day_o +f_month}{$_}; } print "In $year, Day $day_of_month does not fall on ", join(", ", @missing_days); } }

Note that I deliberately haven't described the result, as I think that would spoil the fun. To find out, you'll either have to run the above code, or write your own :p

Cheers,
Darren :)


In reply to How preparing the weekly shift roster led to a fascinating discovery... by McDarren

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.