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)
where
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).
e.g.
>>> day_of_week(9,5,2010)
'Sunday'
>>> day_of_week(23,1,2010)
'Saturday'
>>> day_of_week(23,2,2010)
'Tuesday'
Code:
def day_of_week(d,m,y):
day_names =['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
if m<3:
m=m+12
y=y-1
day=(((13*m+3)//5+d+y+(y/4)-(y//100)+(y//400))%7)
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.]
Code:
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.
Code:
def mostUnlucky():
x=0
y=2010
yearlist=[y for y in range (x,y)]
mostUnlucky=[]
for i in yearlist:
if len(unlucky(i))>=3:
mostUnlucky=[i]+mostUnlucky
return mostUnlucky