Zeller’s Congruence using Python

Posted by Samath
March 25, 2015


Zeller’s Congruence is an algorithm for finding the day of the week for any date. Zeller’s formula is as follows:

day = (((13*m+3) / 5 + d + y + (y /` 4) - (y / 100) + (y / 400)) %7)
d = day, y = year and m = month

Note: If the month is January or February then you add 12 to the month and subtract 1 from the year before calculating the day.

The result is a day number in the range 0..6 where the corresponding day can be extracted from the day_names list by using an appropriate index.

e.g. day_names[0] = ‘Monday’ and day_names[6] = ‘Sunday’.

Define a python function day_of_week, which displays the day name for a given date supplied in the form (day,month,year).

>>> day_of_week(9,5,2010)
>>> day_of_week(23,1,2010)
>>> day_of_week(23,2,2010)


def day_of_week(d,m,y):

    day_names =['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
    if m<3:
    return day_names[int(day)]

Using list comprehension, define a python function unlucky, which returns all the days in a given year which have the date Friday 13th e.g.

>>> unlucky(2010)
[(13, 8, 2010)]
>>> unlucky(2009)
[(13, 2, 2009), (13, 3, 2009), (13, 11, 2009)]

[Hint: you need two ranges one for day starting from 1 and going to 31 and another one for month starting from 1 going to 12. Using these and the year which comes as an argument and use the function day_of_week in the if part of list comprehension to check if a given date is ‘Friday’ and also check if the day is equal to 13.]


def unlucky(y):
    bad=[(d,m,y) for d in range (1,32) for m in range(1,13) if d==13 and day_of_week(d,m,y)=='Friday']
    return bad

Write a python function mostUnlucky, which lists all the years between 0 and 2010 which have 3 unlucky days. Use function unlucky to get a list of unlucky dates for a particular year and find the length of this list. If the length is greater than 2 then the year is added to another list which is returned as output.


def mostUnlucky():
    yearlist=[y for y in range (x,y)]
    for i in yearlist:
        if len(unlucky(i))>=3:
    return mostUnlucky