Python question! Another Date client: using the dict object for the previous dat
ID: 3869527 • Letter: P
Question
Python question!
Another Date client: using the dict object
for the previous date function please refer to https://www.chegg.com/homework-help/questions-and-answers/python-question-date-clients-previous-problem-ps11pr2-please-refer-https-wwwcheggcom-homew-q23350355
Write a function nye_counts(start, end) that counts how many times New Year’s 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 Year’s 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:
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:
where you fill in the blank with an appropriate expression.
The body of the loop should:
Create a Date object to represent New Year’s 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 Year’s 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.
Here’s another example you can use for testing:
Explanation / Answer
ps9pr2.py
from ps9pr1 import Date
def nye_counts(start, end):
"""Insert docstring here."""
counts = {}
counts['Sunday'] = 0
counts['Monday'] = 0
counts['Tuesday'] = 0
counts['Wednesday'] = 0
counts['Thursday'] = 0
counts['Friday'] = 0
counts['Saturday'] = 0
for year in range(start, end + 1):
date =Date(12, 31, year)
#print(date, date.day_of_week())
stri = date.day_of_week()
counts[stri] += 1
print(counts)
def test_nye():
print(nye_counts(2014, 2113))
ps9pr1.py
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 __str__(self):
"""This method returns a string representation for the
object of type Date that calls it (named self).
** Note that this _can_ be called explicitly, but
it more often is used implicitly via printing.
"""
s = "%02d/%02d/%04d" % (self.month, self.day, self.year)
return s
def __eq__(self,other):
return (self.year == other.year) and (self.month == other.month) and (self.day == other.day)
def is_leap_year(self):
""" Returns True if the calling 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 calling object (self).
"""
new_date = Date(self.month, self.day, self.year)
return new_date
def tomorrow(self):
""" adds one day to the current date"""
days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
i = self.month
if self.is_leap_year():
if i == 2:
days_in_month[i] = 29
if (self.day + 1) > days_in_month[i]:
self.day = 0
if i == 12:
self.month = 0
self.year += 1
self.month += 1
self.day += 1
days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
#######
def add_n_days(self, n):
"""adds n days to the current date, n must be a positive integer"""
while n != 0:
print(self)
if n >0:
n -= 1
self.tomorrow()
print(self)
def is_before(self, other):
"""tests to see if self is before other"""
if self.year < other.year:
return True
if self.year > other.year:
return False
if self.year == other.year:
if self.month < other.month:
return True
if self.month > other.month:
return False
if self.month == other.month:
if self.day < other.day:
return True
if self.day >= other.day:
return False
def is_after(self, other):
""" tests is self is after other on the calender """
if self == other:
return False
if self.is_before(other):
return False
else:
return True
def diff(self, other):
"""returns the difernce between self and other (in days) """
dif = 0
selfd = self.copy()
otherd = other.copy()
if self == other:
return 0
if self.is_after(other):
while True:
if selfd == otherd:
return dif
else:
dif += 1
otherd.tomorrow()
if self.is_before(other):
while True:
if selfd == otherd:
return (dif * -1)
else:
dif += 1
selfd.tomorrow()
def test(self):
for i in range(100):
if i == 100:
break
self.day_of_week()
self= self.tomorrow()
def day_of_week(self):
"""returns the day of the week that the day falls on"""
day_of_week_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
dif_day = Date(11,10,2014)
days = self.diff(dif_day)
#weeks = days //7
# print(days)
days = days % 7
#print(days)
day = day_of_week_names[days]
return str(day)
def test_tomorow():
"""tests tomorow function"""
d = Date(12, 31, 2013)
d.tomorrow()
dcheck = Date( 1 ,1 ,2014)
print ('test for new year', dcheck == d)
d = Date(2, 28, 2016)
d.tomorrow()
dcheck = Date(2,29,2016)
print('test for leap-year', d == dcheck)
print(d)
print(dcheck)
def test_addn():
"""tests addn function """
d = Date(11, 10, 2014)
d.add_n_days(3)
dcheck = Date(11,13,2014)
print(dcheck == d)
def test_before():
"""tests before function"""
ny = Date(1, 1, 2015)
d = Date(11, 10, 2014)
print(ny.is_before(d) == False)
print(True == d.is_before(ny))
print(False == d.is_before(d))
def test_after():
"""tsts after function """
ny = Date(1, 1, 2015)
d = Date(11, 10, 2014)
print(ny.is_after(d) == True)
print(False == d.is_after(ny))
print(False == d.is_after(d))
def test_diff():
"""tests diff function """
if 0 == 0:
d1 = Date(11, 10, 2014)
d2 = Date(12, 19, 2014)
print(d2.diff(d1))
print(39)
print(d1.diff(d2))
print(-39)
if 0 == 0:
d3 = Date(12, 1, 2015)
d4 = Date(3, 15, 2016)
print( d4.diff(d3))
print(105)
def day_week():
""" tests day_ofweek """
print(Date(11, 10, 2014).day_of_week() == 'Monday') and print(Date(1, 1, 2100).day_of_week() =='Friday')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.