The question is as below:
Donald John Trump (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.In the history of the United States there have been only four periods when this was the case:
March 4, 1861 - January 18, 1862: Martin Van Buren, John Tyler, Millard Fillmore, Franklin Pierce, James Buchanan
January 20, 1993 - April 22, 1994: Richard Nixon, Gerald Ford, Jimmy Carter, Ronald Reagan, George H. W. Bush
January 20, 2001 - June 5, 2004: Gerald Ford, Jimmy Carter, Ronald Reagan, George H. W. Bush, Bill Clinton
January 20, 2017 - November 30, 2018: Jimmy Carter, George H. W. Bush, Bill Clinton, George W. Bush, Barack Obama
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).
In this problem 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 link shows the contents of a tecxt file that contains information about the last ten Presidents of the United States.
[1]: https://medusa.ugent.be/en/activities/58851522/description/kwhRYw8usry-8zoF/media/us_presidents.txt
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.
I need to write a function headsOfState
that takes the location of a text file that contains information about the lifespan and term of the heads of a particular state. The function must return a dictionary that maps the names of all heads of state in the file onto a tuple with the dates (datetime.date objects) of the four events mentioned in the file, in the same order of appearance as in the file. Dates of events that have not yet occurred must be represented by the value None
.
Below is my code:
from datetime import date
def headsOfState(filepath_of_workdir):
open_file = open(filepath_of_workdir, 'r', encoding='utf-8')
'''accessesing the text file from working directory. here we are \
creating a list of lists of all words line wise using (readlines)'''
content = open_file.readlines()
open_file.close()
content_list = []
for i in range(len(content)):
content_list.append(content[i].split('\t'))
prez_list = []
for i in range(len(content_list)):
prez_list.append(content_list[i][0])
del (content_list[i][0])
#print(prez_list)
#print(content_list)
temp_date = None
inter_date_list = []
final_date_list = []
for i in range(len(content_list)):
temp_date = (content_list[i])
for j in range(len(temp_date)):
item1 = temp_date[j].strip()
item2 = item1.split('/')
if item2 == '' or item2 == ['\n']:
inter_date_list.append(None)
else:
year = int(item2[2])
month = int(item2[1])
day = int(item2[0])
inter_date_list.append(date(year, month, day))
if len(inter_date_list) == len(content_list[i]):
final_date_list.append(inter_date_list)
temp_date = None
inter_date_list = []
dict_prez = dict(zip(prez_list, final_list))
return dict_prez
I am getting the below error:
headsOfState('us_presidents.txt')
Traceback (most recent call last):
File "<ipython-input-66-1730ba5bcf8b>", line 1, in <module>
headsOfState('us_presidents.txt')
File "<ipython-input-65-1fbaf479e49a>", line 28, in headsOfState
year = int(item2[2])
IndexError: list index out of range
The output should look like:
>>> events = headsOfState('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)
Kindly help me in resolving the error or suggest a better strategy.