<jupyter_start><jupyter_text>All the Presidents' Ages<jupyter_code># Run this cell to set up the notebook, but please don't change it.

# These lines import the Numpy and Datascience modules.
import numpy as np
from datascience import *

# These lines do some fancy plotting magic
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
import warnings
warnings.simplefilter('ignore', FutureWarning)<jupyter_output><empty_output><jupyter_text>As of this writing, the US has had 43 Presidents, and 38 are deceased.  Let's figure out how long they lived.First, a note.  These exercises are designed to give you practice *computing with arrays*.  Since there are only 38 Presidents, you could avoid using arrays by copying each computation 38 times.  You wouldn't learn much, so don't do that.Our data from [PresidentsUSA.net](http://www.presidentsusa.net/birth.html) tell us the birth and death date of each President.  The cell below loads these data, along with the Presidents' names.  (We've used a table for presentation purposes; you don't need to know about tables to do this exercise.)Note that the Presidents are presented in order by *birth date*, so for example <NAME> (President from 1961-1963) comes after <NAME> (President from 1969-1974) because he was born earlier.<jupyter_code># Just run this cell.

presidents = Table.read_table("presidents.csv").select("Name", "Birth Year", "Death Year")
# This is an array of the birth years of the dead presidents.  It's the data
# you see displayed in the "Birth Year" column below.
birth_years = presidents.column("Birth Year")
# This is an array of the death years of the dead presidents.  It's the data
# you see displayed in the "Death Year" column below.  The first element of
# this array describes the same president as the first element of birth_years,
# and so on.
death_years = presidents.column("Death Year")
presidents.show()<jupyter_output><empty_output><jupyter_text>**Question 1.** Compute the number of years between each President's birth and death (their longevity).  Put your answers in an array called `longevity`.  The first item of `longevity` should be the longevity of the first president in `dead_presidents_years`, and so on.  Use the arrays `death_years` and `birth_years`, which are loaded in the cell above.<jupyter_code>longevity = ...

# This piece of code puts your results into a table for better
# display.  You can ignore it.
presidents.with_column("Longevity", longevity).show()<jupyter_output><empty_output><jupyter_text>Below, we've plotted the longevity of each president, which you just computed.<jupyter_code># Just run this cell.
Table().with_columns(
        "President number (by birth date)", np.arange(1, presidents.num_rows+1),
        "longevity (years)", longevity)\
    .scatter(0)<jupyter_output><empty_output><jupyter_text>**Question 2.** Suppose each President were [still alive](http://futurama.wikia.com/wiki/Richard_M._Nixon's_head) in 2016.  How old would each one be?<jupyter_code>ages_today = ...

# This piece of code puts your results into a table for better
# display.  You can ignore it.
presidents.with_column("Age Today", ages_today).show()<jupyter_output><empty_output><jupyter_text>**Question 3.** A colleague points out that <NAME> died at age 90, but your answer to Question 1 probably says that he lived 91 years!  <NAME> was born October 30, 1735, and died July 4, 1826.  Explain the discrepancy. *Write your answer here, replacing this text.* Let's fix this.  Below, we've loaded a more precise dataset.  Instead of just birth year and death year, we also have the number of *days* that passed since January 1 of those years.  If someone was born on the 200th day of the year and died on the 100th day of the year, then their birthday hadn't already passed, so we should decrease their longevity by 1.<jupyter_code># Just run this cell.
detailed_ages = Table.read_table("presidents.csv").select("Name", "Birth Year", "Days since January 1 at Birth", "Death Year", "Days since January 1 at Death")
birth_days = detailed_ages.column("Days since January 1 at Birth")
death_days = detailed_ages.column("Days since January 1 at Death")
detailed_ages<jupyter_output><empty_output><jupyter_text>**Question 4.** For each President, compute how many more days passed before their death in their year of death than before their birth in their year of birth.  For example, that number for George Washington is 295, and for <NAME> it's -118.  We'll call this number the "net additional life days."<jupyter_code>net_additional_life_days = death_days - birth_days

# This piece of code puts your results into a table for better
# display.  You can ignore it.
detailed_ages.with_column("Net Additional Life Days", net_additional_life_days)<jupyter_output><empty_output><jupyter_text>To get each President's actual age at death, we should subtract 1 from the longevity of Presidents whose net additional life days are negative.  One way to do this is:* Divide each net additional life day amount by 366 to get a fraction of a year.* Round each fraction down to the nearest integer, using the function `np.floor`.  (`np.floor` takes as its argument an array of numbers.  It returns an array of those numbers rounded down to the nearest integer.)* Add the result to each President's longevity.**Question 5.** Compute each President's actual longevity by following the steps above.*Hint 1:* Use the arrays you've already calculated in previous questions.*Hint 2:* Our answer uses a single line with a compound expression, but you may find it simpler to perform each of the three steps on its own line, giving a name to each intermediate result so you can use it on the next line.<jupyter_code>true_longevity = ...

# This piece of code puts your results into a table for better
# display.  You can ignore it.
detailed_ages.with_column("True Longevity", true_longevity).show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07754279959718026, 'fraction_numerical': 0.012755958375293723, 'mean_word_length': 5.013118062563068, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '147636', 'license': 'MIT', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1610, 'n_words': 792, 'path': 'hw3/president_ages.ipynb', 'repo_name': 'marctollis/INF110-Discovering-Informatics', 'size': '5989'}