Advertisement
hexalogy

Lab 4

Sep 11th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.67 KB | None | 0 0
  1. """
  2. Date Handling
  3. """
  4. import datetime
  5.  
  6. def name_of_weekday(date):
  7.     """What is the name of the weekday?
  8.  
  9.    No work needed.  But, notice that:
  10.    1. date.weekday() returns an index 0 through 6.
  11.    2. we use that index to get a name.
  12.    """
  13.     weekday_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  14.     return weekday_names[date.weekday()]
  15.  
  16. #--------------------------------------------------------------
  17. # IMPLEMENT THE FUNCTIONS BELOW THIS LINE
  18.  
  19. def text_to_ymd(text):
  20.     """
  21.    Convert text into a year, month, day
  22.      ISO: YYYY-MM-DD
  23.    """
  24.     # 1. split the text on "-":
  25.     #     year, month, day = text.split("-")
  26.     # 2. Convert all three values into ints
  27.     # 3. Return the three values: year, month, day
  28.  
  29.     year, month, day = text.split("-")
  30.    
  31.     # REPLACE THIS LINE:
  32.     return int(year), int(month), int(day)
  33.  
  34. def new_years_day():
  35.     """
  36.    What day of the week was this year's January 1st?
  37.    """
  38.  
  39.     # 1. Get today's date:
  40.     #      today = datetime.date.today()
  41.     # 2. get the year: today.year
  42.     # 3. Make and return new date object:
  43.     #      return datetime.date(y, m, d) (pick y, m, d appropriately)
  44.    
  45.     today = datetime.date.today()
  46.     year = today.year
  47.  
  48.     # REPLACE THIS LINE:
  49.     return datetime.date(year, 1, 1)
  50.  
  51. def n_weeks_after_newyears(n_weeks):
  52.     """What day was n_weeks weeks after new year's day?
  53.    """
  54.  
  55.     # 1. Call new_years_day() to get the first date of this year.
  56.     # 2. Call datetime.timedelta( X ), which is an interval.
  57.     #    X is the number of days. (remember 7 days per week).
  58.     # 3. Add that to the first date, and return
  59.  
  60.     first_date = new_years_day()
  61.     week = datetime.timedelta(n_weeks * 7)
  62.     result = first_date + week
  63.    
  64.     # REPLACE THIS LINE:
  65.     return result
  66.  
  67. def first_thursday(month):
  68.     """What was the first Thursday of the given month (this year)?
  69.    """
  70.  
  71.     # 1. Get today, and get today.year to get this year.
  72.     # 2. make a date = datetime.date(year, month, 1) for first day of month.
  73.     # 3. Loop:
  74.     #      while True:
  75.     # 4. inside loop, check if date.weekday() == 3
  76.     #      if so, it's Thursday.  break.
  77.     # 5. otherwise, add one day:
  78.     #      date += datetime.timedelta(1)
  79.     # when done, return date
  80.  
  81.     today = datetime.date.today()
  82.     year = today.year
  83.     date = datetime.date(year, month, 1)
  84.     while True:
  85.         if date.weekday() == 3:
  86.             break
  87.         else:
  88.             date += datetime.timedelta(1)
  89.        
  90.  
  91.     # REPLACE THIS LINE:
  92.     return date
  93.  
  94. def thanksgiving_thursday():
  95.     """What day is the third Friday of November, this year?
  96.    """
  97.     # 1. Get first Thursday of November (call first_thursday(11))
  98.     # 2. add 14 days to it, and return
  99.    
  100.     x = first_thursday(11)
  101.     x += datetime.timedelta(14)
  102.  
  103.     # REPLACE THIS LINE:
  104.     return x
  105.  
  106. def days_until_thanksgiving():
  107.     """How many days from today until this year's thanksgiving?
  108.    """
  109.     # 1. Call thanksgiving Thursday
  110.     # 2. Subtract today's date:
  111.     #  delta = thanksgiving_thursday() - datetime.date.today()
  112.     # 3. return delta.days
  113.    
  114.     thanksgiving_thursday()
  115.     delta = thanksgiving_thursday() - datetime.date.today()
  116.  
  117.     return delta.days
  118.  
  119. def election_day(year):
  120.     """When is election day, on the given year?
  121.    It's the first Tuesday AFTER the first Monday of November.
  122.    """
  123.     # You can do it!
  124.     # FIND THE FIRST MONDAY OF NOV
  125.     # THEN ADD ONE DAY
  126.    
  127.     #today = datetime.date.today()
  128.     #year = today.year
  129.     date = datetime.date(year, 11, 1) #2016 Nov 1st
  130.     while True:
  131.         if date.weekday() == 3: #IF IT FINDS THURSDAY
  132.             date += datetime.timedelta(3) #ADD 3 DAYS WHIC IS MONDAY
  133.             break
  134.         else:
  135.             date += datetime.timedelta(1)
  136.    
  137.     # REPLACE THIS LINE:
  138.     return date
  139.  
  140. def is_leap_year(year):
  141.     """Is the given year a leap year?
  142.    """
  143.     if year % 400 == 0:
  144.         return True
  145.     if year % 100 == 0:
  146.         return False
  147.     if year % 4 == 0:
  148.         return True
  149.     else:
  150.         return False
  151.     """
  152.    Rules (proleptic Gregorian calendar):
  153.    1. If the year is not divisible by 4, it's NOT a leap year.
  154.       return False
  155.    2. if the year IS divisible by 400, it's NOT a leap year.
  156.    3. otherwise, it's a leap year.
  157.    """
  158.  
  159.     # REPLACE THIS LINE:
  160.     return None
  161.  
  162. # IMPLEMENT THE FUNCTIONS ABOVE THIS LINE
  163. #-------------------------------------------------------------
  164.  
  165. def main():
  166.     for text_date in ["2008-09-01", "2015-08-23"]:
  167.         year, month, day = text_to_ymd(text_date)
  168.         date = datetime.date(year, month, day)
  169.         print("{:<22} : {}".format(text_date, date))
  170.     print("New Year's Day          ", new_years_day())
  171.     print("New Year's was a        ", name_of_weekday(new_years_day()))
  172.     print("10 weeks later is a     ",
  173.           name_of_weekday(n_weeks_after_newyears(10)))
  174.     print("First Thursday of Sept: ", first_thursday(9))
  175.     print("Thanksgiving:           ", thanksgiving_thursday())
  176.     print("Days until thanksgiving:", days_until_thanksgiving())
  177.     election = election_day(2016)
  178.     print("Election day 2016:      ", election)
  179.     print("Election day was a      ", name_of_weekday(election))
  180.  
  181.     for decade in range(1980, 2020, 10):
  182.         for year in range(decade, decade + 10):
  183.             print("{:>4} ".format(year), end = "")
  184.         print()
  185.         for year in range(decade, decade + 10):
  186.             if is_leap_year(year):
  187.                 print("{:>4} ".format("Leap"), end = "")
  188.             else:
  189.                 print("{:>4} ".format(""), end = "")
  190.         print()
  191.  
  192. if __name__ == '__main__':
  193.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement