Donald John Trump (born June 14, 1946) has taken office as the 47th President of the United States on January 20, 2025. Since that day, four former American presidents are alive at the same time: Bill Clinton, George W. Bush, Barack Obama, and Joe Biden.

presidents

In the history of the United States there have been only five periods when five former US presidents were still alive at the same time:

Herbert Hoover lived another 11,553 days (31 years and 230 days) after leaving office. James Polk died only three months (103 days) after leaving his presidency. Of the individuals elected as US President, eight never obtained the status of "former president" because they died in office: William H. Harrison (pneumonia), Zachary Taylor (bilious diarrhea), Abraham Lincoln (assassinated), James A. Garfield (assassinated), William McKinley (assassinated), Warren G. Harding (heart attack), Franklin D. Roosevelt (cerebral hemorrhage) and John F. Kennedy (assassinated).

Assignment

We will process text files that contain information about the lifespan and term of the heads of a particular state. Each line contains five tab-separated information fields: i) name of head of state, ii) birth date, iii) (first) term start date, iv) (last) term end date and v) death date. The four date fields are given in the format dd/mm/yyyy with each fragment being a natural number without leading zeroes: dd indicates the day, mm the month and yyyy the year. The following example shows the contents of such a file that contains information about the last ten Presidents of the United States.

Richard Nixon	9/1/1913	20/1/1969	9/8/1974	22/4/1994
Gerald Ford	14/7/1913	9/8/1974	20/1/1977	26/12/2006
Jimmy Carter	1/10/1924	20/1/1977	20/1/1981	29/12/2024
Ronald Reagan	6/2/1911	20/1/1981	20/1/1989	5/6/2004
George H. W. Bush	12/6/1924	20/1/1989	20/1/1993	30/11/2018
Bill Clinton	19/8/1946	20/1/1993	20/1/2001	
George W. Bush	6/7/1946	20/1/2001	20/1/2009	
Barack Obama	4/8/1961	20/1/2009	20/1/2017	
Joe Biden	20/11/1942	20/1/2021	20/1/2025	
Donald Trump	14/6/1946	20/1/2017		

In case a head of state has served multiple non-consecutive terms, we make the assumption that he has served only one consecutive term that runs from the start date of the first term until the end date of the last term. In case the head of state is still serving today, the end date of his term is represented by an empty string. In case the head of state is still alive today, the death date is represented by an empty string. Your task:

Example

In the following interactive sessions we assume the text file us_presidents.txt to be located in the current directory.

>>> events = heads_of_state('us_presidents.txt')
>>> events['George Washington']
(datetime.date(1732, 2, 22), datetime.date(1789, 4, 30), datetime.date(1797, 3, 4), datetime.date(1799, 12, 14))
>>> events['Barack Obama']
(datetime.date(1961, 8, 4), datetime.date(2009, 1, 20), datetime.date(2017, 1, 20), None)
>>> events['Donald Trump']
(datetime.date(1946, 6, 14), datetime.date(2017, 1, 20), None, None)

>>> retirement('Herbert Hoover', events)
11553
>>> retirement('James K. Polk', events)
103
>>> retirement('Bill Clinton', events)

>>> retirement('Abraham Lincoln', events)
0
>>> retirement('Donald Trump', events)
0
>>> retirement('Donald Duck', events)
Traceback (most recent call last):
AssertionError: unknown head of state

>>> import datetime
>>> alive(events)
{'Donald Trump', 'Joe Biden', 'Barack Obama', 'Bill Clinton', 'George W. Bush'}
>>> alive(events, former=True)
{'Joe Biden', 'Barack Obama', 'Bill Clinton', 'George W. Bush'}
>>> alive(events, former=True, reference=datetime.date(2017, 1, 20))
{'George H. W. Bush', 'George W. Bush', 'Jimmy Carter', 'Bill Clinton', 'Barack Obama'}
>>> alive(events, former=True, reference=datetime.date(2016, 12, 31))
{'George W. Bush', 'George H. W. Bush', 'Bill Clinton', 'Jimmy Carter'}
>>> alive(events, former=True, reference=datetime.date(1861, 12, 31))
{'Martin Van Buren', 'Franklin Pierce', 'Millard Fillmore', 'James Buchanan', 'John Tyler'}
>>> alive(events, former=True, reference=datetime.date(1994, 3, 26))
{'Jimmy Carter', 'Ronald Reagan', 'Gerald Ford', 'Richard Nixon', 'George H. W. Bush'}
>>> alive(events, former=True, reference=datetime.date(2003, 10, 12))
{'Gerald Ford', 'Jimmy Carter', 'George H. W. Bush', 'Bill Clinton', 'Ronald Reagan'}

Epilogue

This is a graphical lifespan timeline of all Presidents of the United States. Forty-five persons have served as President of the United States since the office came into existence in 1789. They are listed in order of office (Grover Cleveland and Donald Trump are the only presidents in American history to serve two non-consecutive terms in office; they are listed in the order of their first presidency).

lifespan

Former living presidents rarely come together. This happens most often at inaugurations of new presidents. But on October 21st, 2017 the five former presidents Jimmy Carter, George H.W. Bush, Bill Clinton, George W. Bush and Barack Obama came together at a benefit concert to raise funds for the victims of the hurricanes Harvey, Irma and Maria.

five former American presidents