Advertisement
ruhan008

Python Assignment 1

Jan 15th, 2024
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. # 1 Jan 2001 was Monday
  2. week_day = 1  # 1 based indexing, so 1 -> Monday, 2 -> Tuesday, ..., 7 -> Sunday
  3. month_days = 31  # January has 31 days
  4. month = 1  # 1 -> January, 2 -> February, ..., 12 -> December
  5. year = 2001
  6.  
  7. req_number_of_sundays = 0
  8.  
  9. while year != 2101:
  10.     if (month % 2 == 1 and month <= 7) or (month % 2 == 0 and month > 7):
  11.         month_days = 31
  12.     else:
  13.         if month == 2:
  14.             if year % 4 == 0:
  15.                 month_days = 29
  16.             else:
  17.                 month_days = 28
  18.         else:
  19.             month_days = 30
  20.  
  21.     for day in range(1, month_days + 1):
  22.         if week_day > 7:
  23.             week_day = 1
  24.         if day == 1 and week_day == 7:
  25.             req_number_of_sundays += 1
  26.         week_day += 1
  27.  
  28.     month += 1
  29.     if month > 12:
  30.         month = 1
  31.         year += 1
  32.  
  33. print("The number of Sundays are: " + str(req_number_of_sundays))
  34.  
  35. # Output:
  36. #
  37. # The number of Sundays are: 172
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement