Python Create a function that will use a nested conditional
Python. Create a function that will use a nested conditional structure in order to sort the dates into three categories: future, present, and past. Use the January and 2016 as the present date. In your main program, output the dates into the three categories (future, present, past) as well as the number of dates in each category.
e.g) input.txt
January 2015
May 2016
June 2011
December 2020
January 2016
e.g) output.txt
Future: 2
May 2016
December 2020
Present: 1
January 2016
Past: 2
January 2015
June 2011
Solution
PROGRAM CODE:
def sortDate(array, number): # Function starting
 Dates = []
 for value in array
 mon_year = value
 mon_year = mon_year.split() # splitting to get the month and year separately
 if(number == 1): # Checking for past
 if(mon_year[1]<2016):
 Dates.append(value)
 if(number == 2): # checking for present
 if(mon_year[1] == 2016 & mon_year[0] == \"January\"):
 Dates.append(value)
 if(number == 3): # checking for Future
 if((mon_year[1] == 2016 & mon_year[0] >= \"January\")|(mon_year[1] > 2016)):
 Dates.append(value)
 return Dates #Function Ending
   
 DatesArray = []
 with open(\"file.txt\", \"r\") as ins: # reading from file
 for line in ins:
 DatesArray.append(line) # storing in array
      
 past = sortDate(DatesArray, 1) # 1 - represents past
 present = sortDate(DatesArray, 2) # 2 - represents present
 future = sortDate(DatesArray, 3) # 3 - represents future
 file = open(\"output.txt\", \"w\") # writing to a file
#Printing future
 file.write(\"Future: \"+len(future)+\"\ \")
 for x in future:
 file.write(x+\"\ \")
 #Printing present
 file.write(\"Present: \"+len(present)+\"\ \")
 for y in present:
 file.write(y+\"\ \")
 #Printing past
 file.write(\"Future: \"+len(past)+\"\ \")
 for z in past:
 file.write(z+\"\ \")
 file.close()
OUTPUT:
Future: 2
 May 2016
 December 2020
 Present: 1
 January 2016
 Past: 2
 January 2015
 June 2011


