Question

In: Computer Science

Python Programming #1 Below is an English algorithm for determining the day of the week (Sunday,...

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 (%).

Solutions

Expert Solution

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 :)


Related Solutions

C Question! Problem: Given a day of the week (1-7 corresponding to Sunday through Saturday), the...
C Question! Problem: Given a day of the week (1-7 corresponding to Sunday through Saturday), the month number (1-12), and the year, determine whether that given month has five occurrences of the day of the week and display those dates. Example Execution #1: Enter day of week (1-7) -> 4 Enter month of the year -> 10 Enter the year -> 2019 Finding: There exists five Wednesday dates in October of 2019. Dates: 2 9 16 23 30 Example Execution...
Python English algorithm explanation Write a program that asks the user for the name of a...
Python English algorithm explanation Write a program that asks the user for the name of a file in the current directory. Then, open the file and process the content of the file. 1)If the file contains words that appear more than once, print “We found duplicates.” 2)If the file does not contain duplicate words, print “There are no duplicates.”
Python English algorithm explanation Write a program that asks the user for the name of a...
Python English algorithm explanation Write a program that asks the user for the name of a file in the current directory. Then, open the file and process the content of the file. 1)If the file contains words that appear more than once, print “We found duplicates.” 2)If the file does not contain duplicate words, print “There are no duplicates.”
Homework – Zeller’s Algorithm Pt2 Zeller’s algorithm computes the day of the week on which a...
Homework – Zeller’s Algorithm Pt2 Zeller’s algorithm computes the day of the week on which a given date will fall (or fell). You are provided this algorithm in CANVAS. In this task, you must transform the provided solution to a “Defined Function” Function Name zelleralg Input Parameters • month • day • year Return Values The result from the algorithm. This is a number (0-6) that corresponds to the day of the week, where 0 represents Sunday, 1 is Monday,...
FOR PYTHON: Write a python program for a car salesperson who works a five day week....
FOR PYTHON: Write a python program for a car salesperson who works a five day week. The program should prompt for how many cars were sold on each day and then prompt for the selling price of each car (if any) on that day. After the data for all five days have been entered, the program should report the total number of cars sold and the total sales for the period. See example output. NOTE: duplicate the currency format shown...
Do Programming Exercise 1 (Day of the Week) – Page 151 Output should look like this.....
Do Programming Exercise 1 (Day of the Week) – Page 151 Output should look like this.. >>> =RESTART: C:/Users/gamada/AppData/Local/Programs/python/python36/week/py= what is your name? gamada Enter a number (1-7) for the day of the week: 3 you selected 3 which is wednesday
Build a Dynamic Programming algorithm in Python for the following problem. Given two sequences of vowels,...
Build a Dynamic Programming algorithm in Python for the following problem. Given two sequences of vowels, find the length of longest subsequence present in both of them. NOTE: A subsequence is a sequence that appears in the same order, but not necessarily contiguous. For example, “aei”, “aeo”, “eio”, “aiu”, ... are subsequences of “aeiou”. Sample Input 1: aeiou aiu Sample Output 1: 3 Sample Input 2: aeaiueoiuaeeoeooaauoi aeuioauuuoaeieeaueuiouaiieuiuuuaoueueauaeiauuo Sample Output 2: 16 Sample Input 3: iioioieiaiauaoeoiioiiue iuuueauiaieooaoaaouaaaae Sample Output 3:...
USE PYTHON ONLY Zeller’s congruence is an algorithm developed by Christian Zeller to calculate the day...
USE PYTHON ONLY Zeller’s congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is h = (q + 26(m+1)//10 + k + k//4 +j//4 +5j) % 7 where - h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, 6: Friday). - q is the day of the month. - m is the month (3: March, 4: April, ..., 12: December). January and...
Use Java (Science: day of the week) Zeller’s congruence is an algorithm developed by Christian Zeller...
Use Java (Science: day of the week) Zeller’s congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is : h = (q + 26(m+1)/10 + k + k/4 + j/4 + 5j) % 7 where - h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, 6: Friday). - q is the day of the month. - m is the month (3: March,...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm to sort the digits: Use Counting Sort or Bucket Sort. • Assume the numbers are maximum 4-digit numbers. • If using Counting Sort, you can see that your digit range is between 0 and 9 ([0…9]). • If using Bucket Sort, you will have ten buckets labeled from 0 to 9. Please add comments and explain every step carefully.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT