Yearly Calendar
- You will be writing a program that prints out a calendar for a full
year. The year must not be before 1900 nor beyond 2500.
- You may not use the linux system call to cal, but just so you know
what a month should look like, type cal at the linux prompt.
October 2009
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
At first this seems like a simple task. I'll give you main()
# some constants
MIN_YEAR = 1900
MAX_YEAR = 2500
SUNDAY = 0
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3
THURSDAY = 4
FRIDAY = 5
SATURDAY = 6
def main():
printGreeting()
year = getValidInt("Which year would you like? ", MIN_YEAR, MAX_YEAR )
printCalendar(year)
main()
Step 3 - printMonth()
- Let's look at an example of what we need to print in the
printMonth() function.
October 2009
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Let's take this line by line.
- We need to print the month name and the year.
- Write a stub for a function called monthName(), which
takes the month number and returns the monthName.
- Write a print statement in printMonth() that will print
the returned monthName and the year
- We will always print "Su Mo Tu We Th Fr Sa" so add that
print statement to printMonth()
- This line is a rough one, since the first day of the month isn't
usually a Sunday. We need to know what day of the week the first
day of this month is and we need to indent to that position before
starting to print.
- Let's write a stub for a function called firstDayOfMonth()
that will take the month number and year and return the
weekday number of that day. We'll need to call that function
next.
- Let's write a stub for a function called indentFirstLine()
that will take a weekday and print spaces over to the correct
indentation for that line. We'll need to call this function
next.
The remainder of the printing of the calendar will be accomplished
using a for loop. Whenever the weekday is a Saturday, we'll use a
print statement that will force the newline to be printed after the
Saturday date.
We'll need to know how many days are in the month in order to know
when to stop. So let's write a stub for a function called daysInMonth(),
which takes the month number and the year and returns the number of
days in that month. We'll need to call this function too.
How are we handling the weekdays ?
Notice that we already have constants for the days of the week, with
SUNDAY being 0. This has a great advantage, since it allows us to
implement the operation of cycling past the end of one week using the
modulus operator. If the variable weekday contains the integer
corresponding to the current day of the week, the expression:
(weekday + n) % 7
indicates the day of the week that occurs n days later. For example,
if today is a Saturday (when weekday has a the value 6), 10 days from today
is a Tuesday, because the expression:
(6 + 10) % 7
evaluates to 2. This formula ends up being quite useful for moving ahead
by one weekday.
weekday = (weekday + 1) % 7
So the code for the loop in this function is:
for day in range(1, numDays + 1):
print "%2d" % (day),
if weekday == SATURDAY:
print
weekday = (weekday + 1) % 7
if weekday != SUNDAY:
print
By making use of stubs, we can write printMonth() in its entirety and
can test it.
What Would Be Next?
The next step would be to write the 5 functions that are currently
stubs, one at a time and test them. For now, let's just think about
how each of these can be implemented.
weekday = (weekday + 365) % 7
indentFirstLine() just prints the appropriate number of spaces
so that the day number gets printed in the appropriate column.
daysInMonth() takes the month and year and uses the old rhyme:
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Except February alone.
Which has twenty-eight, in fine,
And each leap year, twenty-nine.
to determine whether to return 30, 31, 28 or 29.
isLeapYear() takes a year and returns a boolean, whether or not
the year passed in is a leap year.
What is a leap year ?
A regular year has 365 days, with February having 28 days. A leap
year has 366 days, with February having 29 days.
How do know which years are leap years ?
A year that is evenly divisible by 4, but not evenly divisible by
100 is a leap year. A year that is evenly divisible by 400 is a
leap year. All other years are not leap years.