In: Computer Science
Python Programming #1
Below is an English algorithm for determining the day of the week (Sunday, Monday, etc.) given the date (day, month and year).
Take the last two digits of the year.
Divide by 4, discarding any fraction. (keep quotient;
ignore remainder)
Add the day of the month.
Add the month's key value: JFM AMJ JAS OND 144 025 036
146
[eg, Jan = 1; Feb = 4; March =4; etc] & see note [a]
Subtract 1 for January or February of a leap
year.
For a Gregorian date, add 0 for 1900's, 6 for 2000's,
4 for 1700's, 2 for 1800's; for other years, add or subtract
multiples of 400.
see note[c]
For a Julian date, add 1 for 1700's, and 1 for every
additional century you go back.
see note [b]
Add the last two digits of the year.
Divide by 7 and take the remainder.
Now, 1 is Sunday, the first day of the week, 2 is Monday, and so
on.
You have been put in charge of planning the New Year’s Eve party for the Computer Science Department. Your initial task is to determine which day of the week New Year’s Eve will fall on for any given year. To do so, implement the above algorithm in Python with the following abstractions (simplifications):
Assume that the date to be evaluated will always be
December 31st (in step 4, you will always add
6)
Assume that the date will always be Gregorian (you
will always ignore step 7)
Assume that the year will always be in the 2000s (in
step 6, you will always add 6)
Bonus: If you are an experienced coder, feel free to modify
the program to calculate the day of the week for any date, not just
December 31. This will require the use of conditionals for step 4
and step 6.
Submit your code (as a .py file or a plain .txt. file) here on Blackboard. Ensure that your code runs before you submit, and ensure the indentation is preserved in the file.
Hints: start by following the steps manually to ensure you understand the algorithm
create three variables (with appropriate names) and assigning them the numeric values of the day, month, and year (last two digits); use another variable (or more) to hold the intermediate results
remember that remainder is determined using the modulo operation (%).
I have made the program to calculate the day of the week for any date, not just December 31.
Code:
def dayofweek(d, m, y):
"""To return the day of the date"""
# Month Key values
t = [ 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 ]
# Dictionary of days with their respective numbers
day = {0:'SUNDAY', 1:'MONDAY', 2:'TUESDAY', 3:'WEDNESDAY', 4:'THURSDAY', 5:'FRIDAY', 6:'SATURDAY'}
# Computation
y -= m < 3
res = (( y + int(y / 4) - int(y / 100) + int(y / 400) + t[m - 1] + d) % 7)
for key, value in day.items():
if key == res:
return value
# Driver Code
print("Enter the Date (dd,mm,yyyy):\n")
d = int(input())
m = int(input())
y = int(input())
day = dayofweek(d, m, y)
print("The day on the given Date is:",day)
Explanation:
The y -= m < 3
is a nice trick. It creates a
"virtual year" that starts on March 1 and ends on February 28 (or
29), putting the extra day (if any) at the end of the
year; or rather, at the end of the previous year. So for
example, virtual year 2011 began on Mar 1 and will end on February
29, while virtual year 2012 will begin on March 1 and end on the
following February 28.
There are 365 days in a normal year. That is 52 weeks plus 1
day. So the day of the week shifts by one day per year, in general.
That is what the y
term is contributing; it adds one
to the day for each year.
But every four years is a leap year. Those contribute an extra
day every four years. Thanks to the use of virtual years, we can
just add y/4
to the sum to count how many leap days
happen in y
years. (Note that this formula assumes
integer division rounds down.)
But that is not quite right, because every 100 years is not a
leap year. So we have to subtract off y/100
.
Except that every 400 years is a leap year again. So we have to
add y/400
.
Finally we just add the day of the month d
and an
offset from a table that depends on the month (because the month
boundaries within the year are fairly arbitrary).
Then take the whole thing mod 7 since that is how long a week is.
Hope this helps!
Good Luck :)