Question
In Python 3: Write a function nye_counts(start, end) that counts how many times New Years Eve (December 31st) falls on each day of the week
In Python 3:
Write a function nye_counts(start, end) that counts how many times New Years Eve (December 31st) falls on each day of the week between the years start and end, inclusive of both endpoints, and that returns a dictionary containing those counts.
For example, in the three years from 2014 to 2016, New Years Eve falls once on Wednesday, once on Thursday, and once on Saturday, but does not fall on any other day of the week. Thus, your function would return a dictionary with the following contents:
{'Saturday': 1, 'Thursday': 1, 'Monday': 0, 'Sunday': 0, 'Tuesday': 0, 'Wednesday': 1, 'Friday': 0}
Note: The order of the key-value pairs in a dictionary is not guaranteed, so your dictionary may show the days of the week in a different order. That is not a problem!
In ps12pr1.py, we have given you code at the start of the function that initializes the dictionary for you. You need to complete the rest of the funtion.
You should use a for loop to iterate over the years in the range given by the parameters start and end. Your loop header should look something like this:
for year in _____________:
where you fill in the blank with an appropriate expression.
The body of the loop should:
- Create a Date object to represent New Years Eve for the current value of year.
- Call the day_of_week method for that Date object to determine the day of the week on which New Years Eve falls in that year.
- Update the appropriate element of the dictionary.
Finally, once all of the years in the range have been considered, the dictionary should be returned. Make sure that you return it and do not print it.
Heres another example you can use for testing:
>>> counts = nye_counts(2014, 2113) >>> counts {'Monday': 14, 'Tuesday': 13, 'Friday': 14, 'Wednesday': 15, 'Thursday': 15, 'Sunday': 14, 'Saturday': 15} >>> counts['Wednesday'] 15
ps12pr1.py:
# # ps12pr1.py (Problem Set 12, Problem 1) # # More Date clients programs #
# this import statement will allow your to use the Date class from ps11pr2.py from ps11pr2 import Date
def nye_counts(start, end):
d = {} # create an empty dictionary
# add your code here
return d
# end of ps12pr1
ps11pr2:
# # ps11pr2.py (Problem Set 11, Problem 2) # # A class to represent calendar dates #
class Date: """ A class that stores and manipulates dates, represented by a day, month, and year. """
# The constructor for the Date class. def __init__(self, new_month, new_day, new_year): """ The constructor for objects of type Date. """ self.month = new_month self.day = new_day self.year = new_year
# The function for the Date class that returns a Date # object in a string representation. def __repr__(self): """ This method returns a string representation for the object of type Date that it is called on (named self).
** Note that this _can_ be called explicitly, but it more often is used implicitly via printing or evaluating. """ s = '%02d/%02d/%04d' % (self.month, self.day, self.year) return s
def is_leap_year(self): """ Returns True if the called object is in a leap year. Otherwise, returns False. """ if self.year % 400 == 0: return True elif self.year % 100 == 0: return False elif self.year % 4 == 0: return True return False
def copy(self): """ Returns a new object with the same month, day, year as the called object (self). """ new_date = Date(self.month, self.day, self.year) return new_date
#### Put your code for problem 2 below. ####
# 1
def tomorrow(self): """ changes the called object so that it represents one calendar day after the date that it originally represented """ days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if self.is_leap_year() == True: days_in_month = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if self.month == 12 and self.day == 31: self.month = (self.month - self.month) + 1 self.day = (self.day - self.day) + 1 self.year += 1 elif days_in_month[self.month] == self.day and self.month
def yesterday(self): """ previous day """ days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] self.day -= 1 if self.day
# 2
def add_n_days(self, n): """ changes the calling object so that it represents n calendar days after the date it originally represented """ if n == 0: print(self) else: print(self) for i in range(n): self.tomorrow() print(self)
# 3
def __eq__(self, other): """ returns True if the called object (self) and the argument (other) represent the same calendar date. returns False otherwise """ if self.year == other.year and self.month == other.month and self.day == other.day: return True else: return False
# 4
def is_before(self, other): """ returns True if self represents a calendar date that occurs before the calendar date, other if self occurs on or after other, returns False """ if self.__eq__(other) == True: return False elif self.year
# 5
def is_after(self, other): """ returns True if self occurs after the calendar date, other if self occurs before or on other, returns False """ if self.__eq__(other) == True: return False elif self.is_before(other) == True: return False elif self.is_before(other) == False: return True
# 6
def diff(self, other): """ returns an integer that represents the number of days between self and other """ dcopy = self.copy() difference = 0 while dcopy.is_before(other): dcopy.tomorrow() difference -= 1 while dcopy.is_after(other): dcopy.yesterday() difference += 1 return difference
# 7
def day_of_week(self): """ returns a string indicating the day of the week of the Date object that calls it """ day_of_week_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] Reference = Date(4, 4, 2016) differ = self.diff(Reference) return day_of_week_names[(differ % 7)]
# ps11pr2.py (Problem Set 11, Problem 2) # A class to represent calendar dates class Date: A class that stores and manipulates dates, represented by a day, month, and year # The constructor for the Date class def init__(self, new_month, new day, new year) ""The constructor for objects of type Date. self.month new month self.day -new_day self.year -new year # The function for the Date class that returns a Date # object in a string representation. def .repr__(self): "s method returns a string representation for the object of type Date that it is called on (named self) **Note that this can be called explicitly, but it more often is used implicitly via printing or evaluating s- '%02d/%02d/X04d ' return S % (self.month, self.day, self.year) def is_leap year(self): Returns True if the called object is in a leap year. Otherwise, returns False if sel f . year % 400-0: elif self.year % 100--0: elif self.year % 4-. 0: return False return True return False return True def copy(self): "Returns a new object with the same month, day, year as the called object (self) new date Date(self.month, self.day, self.year) return new date #### Put your code for problem 2 below. #### def tomorrow(self): " changes the called object so that it represents one calendar day after the date that it originally represented " days in_month [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if self.is leap_yearOTrue: days_in_month [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if self.month12 and self.day31 self.month-(self.month - self.month) + 1 self.day-(self.day self.day) 1 self.year1 elif days_in_month[self.month]self.day and self.monthStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started