Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import datetime
- import time
- import string
- # https://en.wikipedia.org/wiki/List_of_members_of_the_Quorum_of_the_Twelve_Apostles_(LDS_Church)
- # https://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States
- ##########################################################
- def man_age(a_man, a_date):
- # date of birth
- a_birth = datetime.datetime.strptime(a_man['birth'], '%Y-%m-%d')
- a_age = a_date - a_birth
- age_year, age_day = divmod(a_age.days, 365.25)
- return int(age_year)
- ##########################################################
- def man_age_x(a_man, a_date):
- # date of birth
- a_birth = datetime.datetime.strptime(a_man['birth'], '%Y-%m-%d')
- a_age = a_date - a_birth
- age_year, age_day = divmod(a_age.days, 365.25)
- # round the age to two decimal places
- final_age = age_year + (int(age_day*100/365.25)/100)
- return final_age
- ##########################################################
- def currently_serving_man(a_man, a_date):
- # acquire the date they started as an apostle
- a_start = datetime.datetime.strptime(a_man['start'], '%Y-%m-%d')
- # acquire the date they stopped being an apostle
- if a_man['end'] == "-":
- # apostle is still serving
- a_end = datetime.datetime.now()
- else:
- # apostle is deceased or no longer serving
- a_end = datetime.datetime.strptime(a_man['end'], '%Y-%m-%d')
- # Is the current date on or between those dates
- if (a_date >= a_start) and (a_date <= a_end):
- return True
- else:
- return False
- ##########################################################
- def load_person_list (personfile, person_list):
- p = open(personfile, "r")
- for line in p:
- field = line.split(',')
- man = {
- "name" : field[0],
- "birth" : field[1],
- "start" : field[2],
- "end" : field[3]
- }
- '''
- print('Name: ', field[0], end='')
- print(', Birth: ', field[1], end='')
- print(', Start: ', field[2], end='')
- print(', End: ', field[3])
- '''
- person_list.append(man)
- p.close
- #########################################################################
- # main()
- # File containing a list of people, birthdays, and service dates
- persontextfile="apostle1.txt"
- #persontextfile="supreme.txt"
- # List that will contain a list of people, birthdays, and service dates
- person_array=[]
- load_person_list (persontextfile, person_array)
- for y in range(1835, 2017):
- # build a date for the week after April and October biannual general conference
- apr_gc = datetime.datetime.strptime(str(y) + '-04-15', '%Y-%m-%d')
- oct_gc = datetime.datetime.strptime(str(y) + '-10-15', '%Y-%m-%d')
- apr_man_total = 0
- apr_age_total = 0
- oct_man_total = 0
- oct_age_total = 0
- for man in person_array:
- if currently_serving_man(man, apr_gc):
- apr_man_total += 1
- apr_age_total += man_age(man, apr_gc)
- if currently_serving_man(man, oct_gc):
- oct_man_total += 1
- oct_age_total += man_age(man, oct_gc)
- if apr_man_total > 0:
- apr_average_age = round(apr_age_total / apr_man_total, 1)
- print('Apr %s : %s' % (str(y), str(apr_average_age)))
- if oct_man_total > 0:
- oct_average_age = round(oct_age_total / oct_man_total, 1)
- print('Oct %s : %s' % (str(y), str(oct_average_age)))
- # Initialize counts of men
- total_man_start = 0
- total_age_start = 0
- total_man_end = 0
- total_age_end = 0
- # Initial identity of the youngest man at start.
- youngest_start = ''
- youngest_start_age = 1000
- # Initial identity of the youngest man at end.
- youngest_end = ''
- youngest_end_age = 1000
- # Initial identity of the oldest man at start.
- oldest_start = ''
- oldest_start_age = 0
- # Initial identity of the ldest man at end.
- oldest_end = ''
- oldest_end_age = 0
- for man in person_array:
- date_at_start = datetime.datetime.strptime(man['start'], '%Y-%m-%d')
- # allow for checking only later dates for whatever reason
- earliest_date = '2000-01-01'
- earliest = datetime.datetime.strptime(earliest_date, '%Y-%m-%d')
- if date_at_start >= earliest:
- print(man['name'])
- age_at_start = man_age_x(man, date_at_start)
- if man['end'] == "-":
- # Still alive
- age_at_end = 0
- #print('%s, -----, %s' % (str(age_at_start), man['name']))
- else:
- # Deceased
- date_at_end = datetime.datetime.strptime(man['end'], '%Y-%m-%d')
- age_at_end = man_age_x(man, date_at_end)
- #print('%s, %s, %s' % (str(age_at_start), str(age_at_end), man['name']))
- # Count the number of men
- total_man_start += 1
- # Start date of current man
- start_date = datetime.datetime.strptime(man['start'], '%Y-%m-%d')
- # Age of current man at start
- start_age = man_age_x(man, start_date)
- # Sum those ages
- total_age_start += start_age
- # Check if the current man is older or younger
- # than the oldest or youngest at start
- if start_age < youngest_start_age:
- youngest_start_age = start_age
- youngest_start = man['name']
- if start_age > oldest_start_age:
- oldest_start_age = start_age
- oldest_start = man['name']
- if man['end'] != "-":
- total_man_end += 1
- end_date = datetime.datetime.strptime(man['end'], '%Y-%m-%d')
- end_age = man_age_x(man, end_date)
- total_age_end += end_age
- # Check if the current man is older or younger
- # than the oldest or youngest at end
- if end_age < youngest_end_age:
- youngest_end_age = end_age
- youngest_end = man['name']
- if end_age > oldest_end_age:
- oldest_end_age = end_age
- oldest_end = man['name']
- # Done with comparisons, start calculating and reporting
- print('Stats of people with start dates after', earliest_date)
- if total_man_start > 0:
- avg_start = int(100 * total_age_start / total_man_start)/100
- else:
- avg_start = 'none'
- if total_man_end > 0:
- avg_end = int(100 * total_age_end / total_man_end)/100
- else:
- avg_end = 'none'
- print ('total people : %s' % (str(total_man_start)))
- print ('average age at start : %s' % (str(avg_start)))
- print ('average age at end : %s' % (str(avg_end)))
- print ('')
- print ('youngest at start : %s' % youngest_start)
- print ('age : %s' % str(youngest_start_age))
- print ('')
- print ('youngest at end : %s' % youngest_end)
- print ('age : %s' % str(youngest_end_age))
- print ('')
- print ('oldest at start : %s' % oldest_start)
- print ('age : %s' % str(oldest_start_age))
- print ('')
- print ('oldest at end : %s' % oldest_end)
- print ('age : %s' % str(oldest_end_age))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement