In: Computer Science
I have a python dictionary with the following format
Key Type Value
January str ['January 01 2020', 'January 02 2019', 'January 03 2018']
June str ['June 04 2018', 'June 05 2018', 'June 06 2016]
August str ['Augsut 07 2016', 'August 08 2016']
How do return the following conclusion with python code?
January has the most day (1) in 2020
January has the most day (1) in 2019
June has the most days (2) in 2018
August has the most days (2) in 2016
In case of any query do comment. Thanks
Code:
#function to calculate number of days in the given dictionary for the given month as key and year to serach
def findNumbeOfDays(monthsDictionary, key, year):
count =0
#if month is present in the dictionary
if key in monthsDictionary:
#take out the value part which is a list here
listValue = monthsDictionary[key]
#iterate over a list of values
for item in listValue:
#find if item in the list contains year given then increase the count
if year in item:
count = count +1
return count
def main():
#prepare a dictionary as given
months ={
'January':['January 01 2020', 'January 02 2019', 'January 03 2018'],
'June':['June 04 2018', 'June 05 2018', 'June 06 2016'] ,
'August': ['August 07 2016', 'August 08 2016']
}
#call findNumbeOfDays function as per the given format print the output
print("January has the most day ({}) in 2020".format(findNumbeOfDays(months,'January','2020')))
print("January has the most day ({}) in 2019".format(findNumbeOfDays(months,'January','2019')))
print("June has the most day ({}) in 2018".format(findNumbeOfDays(months,'June','2018')))
print("August has the most day ({}) in 2016".format(findNumbeOfDays(months,'August','2016')))
#main driver function
if __name__ == '__main__':
main()
screen shot of the code for indentation and output: