Exercise
Exercise
This is exercise 7 from chapter 3 of How to Prove It by Daniel J . Velleman .
In[]:=
Resolution
Resolution
We create the lists
In[]:=
p={"Johann Sebastian Bach","Napoleon Bonaparte","Goethe","David Hume","Mozart","Newton","George Washingtong"};y={1750,1751,1752,1753,1754,1755,1756,1757,1758,1759};
We create the entities from the names ...
p1=Interpreter["Person"][#]&/@p
Out[]=
,,,,,,
... and the date objects from the years in the corresponding lists .
y1=DateObject[{#}]&/@y
Out[]=
,,,,,,,,,
Now a list with sublists containing the entity and its life span .
persona={#,DateInterval[{#["BirthDate"],#["DeathDate"]}]}&/@p1
Out[]=
,DateInterval,,DateInterval,,DateInterval,,DateInterval,,DateInterval,,DateInterval,,DateInterval
A better view :
persona//TableForm
Out[]//TableForm=
DateInterval | |
DateInterval | |
DateInterval | |
DateInterval | |
DateInterval | |
DateInterval | |
DateInterval |
Functions
A function that maps each person' s name to identify whether the year "y" overlaps their life span . If it does, it returns the person' s name; if it does not, it returns nothing .
In[]:=
g[y_]:=If[DateOverlapsQ[Flatten[#][[2]],y],Flatten[#][[1]],Nothing]&/@persona
Now we map the function using the list of date objects created earlier . The result is a list of associations .
a=AssociationThread[#,{g[#]}]&/@y1
Out[]=
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
A better view :
a//TableForm
Out[]//TableForm=
|
|
|
|
|
|
|
|
|
|
Union and intersection
Union and intersection
With what we have found we can obtain the union and intersection of the sets .
{"Union of sets"->Union@@Flatten[Values[a],1],"Intersection of sets"->Intersection@@Flatten[Values[a],1]}//TableForm
Out[]//TableForm=
Union of sets |
Intersection of sets |