Advertisement
makispaiktis

Find the day by the date - What day it is?

Oct 10th, 2020 (edited)
1,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.48 KB | None | 0 0
  1. '''
  2. My only data is that in: 01 January of 1970 was Thursday
  3. I have to predict the day of a given set of days/month/year = date
  4. '''
  5. daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  6. daysPerMonthInLeapYear = daysPerMonth.copy()
  7. daysPerMonthInLeapYear[1] = 29
  8. DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
  9. MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
  10.  
  11. # 1st Function
  12. def isLeapYear(year):
  13.     # Examples:
  14.     # 2001 ---> no, 2004 ---> yes, 2060 ---> yes, 2300 ---> no, 2400 ---> yes
  15.     if year % 4 != 0:
  16.         # Like 2001 ---> False
  17.         return False
  18.     # The following cases conclude that: year can be divided exactly by 4 (year % 4 == 0)
  19.     elif year % 4 == 0 and year % 100 != 0:
  20.         # Like 2004 and 2060 ---> True
  21.         return True
  22.     # Now, we know for sure that our year can be divided by 4 and 100 ---> year % 4 == 0 and year % 100 == 0
  23.     elif year % 100 == 0 and year % 400 != 0:
  24.         # Like 2300 ---> False
  25.         return False
  26.     else:           # year % 400 == 0
  27.         # Like 2400
  28.         return True
  29.  
  30.  
  31. # 2nd Function
  32. def allDays(year):
  33.     if isLeapYear(year) == True:
  34.         return 366
  35.     return 365
  36.  
  37. # 3rd Function
  38. def partialDays(days, month, year):
  39.     SUM = 0
  40.     flag = isLeapYear(year)
  41.     if flag == False:
  42.         for i in range(month-1):
  43.             SUM += daysPerMonth[i]
  44.         return SUM + days
  45.     else:
  46.         for i in range(month-1):
  47.             SUM += daysPerMonthInLeapYear[i]
  48.         return SUM + days
  49.  
  50. # 4th Function
  51. def countDays(days, month, year):
  52.     SUM = 0
  53.     for YEAR in range(1970, year):
  54.         SUM += allDays(YEAR)
  55.     SUM += partialDays(days, month, year)
  56.     # This sum INCLUDES the 1st January of 1970 and I want to exclude it in order to find how many days
  57.     # difference there are from 1 January
  58.     return SUM - 1
  59.  
  60. # 5th Function - Find the final day
  61. def giveDAY(number):
  62.     return DAYS[(number + 3) % 7]
  63.     # I added 3, beacuse my list of day begins with Monday (index = 0)
  64.     # and I want to begin my process from Thursday (index = 3) cause of my data
  65.  
  66. # 6th Function - Checking the input
  67. def checkInput(days, month, year):
  68.     if year < 1970:
  69.         print("We have data after: 1 January 1970")
  70.         return False
  71.     elif month < 0 or month > 12 or month != int(month):
  72.         print("Each year has 12 months.")
  73.         return False
  74.     else:
  75.         if days < 0:
  76.             print("There are no negative days as we know")
  77.             return False
  78.         leap = isLeapYear(year)
  79.         if leap == False:
  80.             if days <= daysPerMonth[month-1]:
  81.                 return True
  82.             print("This month has max days = " + str(daysPerMonth[month-1]) + ", but you gave me " + str(days))
  83.             return False
  84.         else:
  85.             # We speak about a leap year
  86.             if days <= daysPerMonthInLeapYear[month-1]:
  87.                 return True
  88.             print("This month has max days = " + str(daysPerMonth[month-1]) + ", but you gave me " + str(days))
  89.             return False
  90.  
  91.  
  92. # 7th Function
  93. def breakDATE(date):
  94.     # There will be 2 spacebar-characters
  95.     index1 = -1
  96.     index2 = -1
  97.     for i in range(len(date)):
  98.         if date[i] == " ":
  99.             index1 = i
  100.             break
  101.     for j in range(len(date)-1, -1, -1):
  102.         if date[j] == " ":
  103.             index2 = j
  104.             break
  105.     if index2 <= index1:
  106.         print("There was a problem using function 'breakDATE'")
  107.         return None
  108.     # Now, we have to separate the infos coming from date
  109.     days = date[0 : index1]
  110.     MONTH = date[index1+1 : index2]
  111.     MONTHFLAG = False
  112.     month = -1000
  113.     year = date[index2+1 :]
  114.     for i in range(len(MONTHS)):
  115.         if MONTH == MONTHS[i]:
  116.             month = i + 1
  117.             MONTHFLAG = True
  118.             break
  119.     if MONTHFLAG == False:
  120.         print("There is no month named '" + str(MONTH) + "'")
  121.     else:
  122.         return int(days), month, int(year)
  123.  
  124.  
  125.  
  126. # MAIN FUNCTION
  127. print("Give me a VALID date matching the format ----> Number Month Year")
  128. print("Example: 27 December 1999")
  129. date = input("Date looking for info: ")
  130. days, month, year = breakDATE(date)
  131. flag = checkInput(days, month, year)
  132. if flag == True:
  133.     print("---- Valid Date ----")
  134. SUM = countDays(days, month, year)
  135. print(str(SUM) + " days away from 1 January 1970")
  136. print(str(date) + " ----> " + str(giveDAY(SUM)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement