Simple textual calendar

Jorgen Grahn grahn+nntp at snipabacken.se
Mon Nov 3 16:33:57 EST 2008


On Mon, 03 Nov 2008 09:17:25 -0600, Tim Chase <python.list at tim.thechases.com> wrote:
>> Yes, I saw the calendar module, but, as I said, this isn't
>> homework or something like that. It is an example which I've
>> setted myself to try to solve to see how it would go. Calendar
>> just gives me the solution :-(
>
> Part of the answer to a problem is knowing when not to duplicate 
> effort that's already been exerted & tested... :)

But this one is a good intellectual exercise which combines puzzle
elements and outside requirements (i.e. what a calendar looks like).

And besides, someone has to write those modules, you know ;-)

I'd start with just one month, and a piece of paper. Assuming Sunday
starts the week:

   s m t w t f s
   . . . . . . .
   . . . . . . .
   . . . . . . .
   . . . . . . .
   . . . . . . .
   . . . . . . .

It's obvious that worst-case you need six rows.  Then I'd pretend that
the day-of-week of the 1st, and the number of days in the month are
inputs to the problem so I don't have to worry about them.  And I'd
pretend that the output is just a list of 7*6 numbers, with 0 for the
unused days (or None, or '').

  def month(weekday1, days):
      # -> list of 42 numbers
      pass

Then I'd pick an example: a month with the 1st on Tuesday (2) and 30
days. That would be [0]*2 + [1 .. 30] + [0]*10. And that more or less
makes the solution (of this sub-problem) obvious. I'd implement it,
write unit tests, and move on to the next part of the problem with a
bit more confidence.

/Jorgen

-- 
  // Jorgen Grahn <grahn@        Ph'nglui mglw'nafh Cthulhu
\X/     snipabacken.se>          R'lyeh wgah'nagl fhtagn!



More information about the Python-list mailing list