Case: Prepare the names of 10 people, and then randomly generate birthdays for these 10 people [all born in the 1990s]
1. Count those who were born in summer [June-August].
2. How many days is the biggest bigger than the smallest
3. Who has the earliest birthday and who has the latest birthday
Remarks: Spring [3-5] Summer [6-8] Autumn [9-11] Winter [12-2]
Demo:
from datetime import date, timedelta
from random import randint
def build_birthday(list_person_name:list):
#Initialize the storage " name: birthday " dictionary
name_birthday ={}.fromkeys(list_person_name)
#generate birthday
for key in name_birthday:
temp_year = randint(1990, 1999)
temp_month = randint(1, 12)
temp_day = randint(1, 30)
name_birthday[key] = date(temp_year, temp_month, temp_day)
#return _
return name_birthday
def person_birthday_summer(name_birthday:dict):
#Used to store keys born in summer
list_person = []
for key in name_birthday:
if name_birthday[key].month >= 6 and name_birthday[key].month <= 8:
list_person.append(key)
#return _
return list_person
def get_person_year_max(name_birthday:dict):
# extract the date of birth from the dictionary
person_birth = list(name_birthday.values())
# get the biggest birthday
max_birthday = sorted(person_birth)[len(person_birth)-1]
#traverse _
for key in name_birthday:
if name_birthday[key] == max_birthday:
return key
def get_person_year_min(name_birthday:dict):
# extract the date of birth from the dictionary
person_birth = list(name_birthday.values())
# get the smallest birthday
min_birthday = sorted(person_birth)[0]
#traverse _
for key in name_birthday:
if name_birthday[key] == min_birthday:
return key
def get_person_days(name_birthday:dict):
# extract the date of birth from the dictionary
person_birth = list(name_birthday.values())
# get the biggest birthday
min_birthday = sorted(person_birth)[0]
max_birthday = sorted(person_birth)[len(person_birth)- 1]
#return days
return (max_birthday-min_birthday).days
def get_person_early_birthday(name_birthday:dict):
for key in name_birthday:
name_birthday[key] = name_birthday[key].replace(year=1990)
person_birth = list(name_birthday.values())
return(sorted(person_birth)[0])
def get_person_later_birthday(name_birthday:dict):
for key in name_birthday:
name_birthday[key] = name_birthday[key].replace(year=1990)
person_birth = list(name_birthday.values())
return(sorted(person_birth)[len(person_birth)-1])
if __name__ == "__main__":
list_name = [ " Zhao Yi " , " Yang Er " , " Zhang San " , " Li Si " , " Wang Wu " , " Zhao Liu " , " Ma Qi " , " Zheng Ba " , " Liu Jiu " , " Hu ten " ]
#Generate birthdays for all students in list_name
name_birthday = build_birthday(list_name)
print(name_birthday)
#call function module
birthday_summer_list =person_birthday_summer(name_birthday)
if len(birthday_summer_list) == 0:
print ( " Nobody's birthday is in summer : " )
else:
print ( "The birthdays are summer: " , birthday_summer_list) #Requirement 1
#need two
print ( " Maximum birthday: " , get_person_year_max(name_birthday))
print ( " Minimum birthday: " , get_person_year_min(name_birthday))
print ( " Maximum to mindays: " , get_person_days(name_birthday))
#demand three
date_early =get_person_early_birthday(name_birthday)
print ( "The biggest birthday is: %d month %d day " %(date_early.month,date_early.day))
date_later = get_person_later_birthday(name_birthday)
print ( "The smallest birthday is: %d month %d day " % (date_later.month, date_early.day))
Results of the:
C:\python\python.exeC:/python/demo/file3.py
{'Zhao Yi':datetime.date(1992, 12, 30), 'Yang Er': datetime.date(1995,6, 23), 'Zhang San': datetime.date(1990, 6, 21), ' Li Si':datetime.date(1991, 9, 29), 'Wang Wu':datetime.date(1996, 2, 26), 'Zhao Liu':datetime.date(1995, 9, 18), 'Ma Qi ':datetime.date(1996, 7, 4), 'Zheng Ba':datetime.date(1990, 3, 5), 'Liu Jiu':datetime.date(1992, 3, 3), 'Hu Shi': datetime.date(1992, 11, 6)}
Those whose birthdays are summer are: ['Yang Er','Zhang San','Ma Qi']
Biggest birthday: Ma Qi
Youngest birthday: Zheng Ba
Maximum than minimum days: 2313
Biggest birthday: February 26
Youngest birthday is: December 26
Process finished with exit code 0