Donald John Trump1 (born June 14, 1946) has taken office as the 45th President of the United States on January 20, 2017. Since that day, five former American presidents are alive at the same time.

presidents
From January 20, 2001 until June 5, 2004 five former presidents of the United States were still alive. From left to right: Bill Clinton, George H. W. Bush, Ronald Reagan, Jimmy Carter and Gerald Ford.

In the history of the United States there have been only four periods when this was the case:

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.

Lyndon B. Johnson	27/8/1908	22/11/1963	20/1/1969	22/1/1973
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	
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	
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	
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.txt2 to be located in the current directory.

>>> events = heads_of_state('us_presidents.txt3')
>>> 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)
5820
>>> 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', 'Jimmy Carter', 'George H. W. Bush', 'Bill Clinton', 'Barack Obama', 'George W. Bush'}
>>> alive(events, former=True)
{'George W. Bush', 'George H. W. Bush', 'Bill Clinton', 'Jimmy Carter'}
>>> 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(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-four 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 is the only president in American history to serve two non-consecutive terms in office; he is listed in the order of his first presidency).

tijdslijn van Amerikaanse presidenten
Tijdslijn van Amerikaanse presidenten.

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. At the time George H. W. Bush was already suffering from some sort of Parkinson's disease, which confined him to a wheelchair.

five former American presidents
Five former American presidents seen in one shot. From left to right: George H. W. Bush, Barack Obama, George W. Bush, Bill Clinton and Jimmy Carter.