Python code , v3.5 , #54 Please use dictionaries if possible. The complete txt f
ID: 3726263 • Letter: P
Question
Python code , v3.5 , #54 Please use dictionaries if possible.
The complete txt file of JusticesDict.dat looks like:
{'Lucius Lamar': {'pres': 'Grover Cleveland', 'yrAppt': 1888, 'yrLeft': 1893, 'state': 'MS'}, 'William Douglas': {'pres': 'Franklin Roosevelt', 'yrAppt': 1939, 'yrLeft': 1975, 'state': 'CT'}, 'William Johnson': {'pres': 'Thomas Jefferson', 'yrAppt': 1804, 'yrLeft': 1834, 'state': 'SC'}, 'John Clarke': {'pres': 'Woodrow Wilson', 'yrAppt': 1916, 'yrLeft': 1922, 'state': 'OH'}, 'Samuel Alito': {'pres': 'George W. Bush', 'yrAppt': 2006, 'yrLeft': 0, 'state': 'NJ'}, 'Ward Hunt': {'pres': 'Ulysses Grant', 'yrAppt': 1872, 'yrLeft': 1882, 'state': 'NY'}, 'Rufus Peckham': {'pres': 'Grover Cleveland', 'yrAppt': 1895, 'yrLeft': 1910, 'state': 'NY'}, 'Roger Taney': {'pres': 'Andrew Jackson', 'yrAppt': 1836, 'yrLeft': 1864, 'state': 'MD'}, 'Thurgood Marshall': {'pres': 'Lyndon Johnson', 'yrAppt': 1967, 'yrLeft': 1992, 'state': 'NY'}, 'Frank Murphy': {'pres': 'Franklin Roosevelt', 'yrAppt': 1940, 'yrLeft': 1949, 'state': 'MI'}, 'Potter Stewart': {'pres': 'Dwight Eisenhower', 'yrAppt': 1959, 'yrLeft': 1981, 'state': 'OH'}, 'Morrison Waite': {'pres': 'Ulysses Grant', 'yrAppt': 1874, 'yrLeft': 1888, 'state': 'OH'}, 'Benjamin Curtis': {'pres': 'Millard Fillmore', 'yrAppt': 1851, 'yrLeft': 1857, 'state': 'MA'}, 'John Roberts': {'pres': 'George W. Bush', 'yrAppt': 2005, 'yrLeft': 0, 'state': 'MD'}, 'James Byrnes': {'pres': 'Franklin Roosevelt', 'yrAppt': 1941, 'yrLeft': 1942, 'state': 'SC'}, 'Thomas Todd': {'pres': 'Thomas Jefferson', 'yrAppt': 1807, 'yrLeft': 1826, 'state': 'KY'}, 'Sonia Sotomayor': {'pres': 'Barack Obama', 'yrAppt': 2009, 'yrLeft': 0, 'state': 'NY'}, 'Edward Sanford': {'pres': 'Warren Harding', 'yrAppt': 1923, 'yrLeft': 1930, 'state': 'TN'}, 'Benjamin Cardozo': {'pres': 'Herbert Hoover', 'yrAppt': 1932, 'yrLeft': 1938, 'state': 'NY'}, 'Samuel Chase': {'pres': 'George Washington', 'yrAppt': 1796, 'yrLeft': 1811, 'state': 'MD'}, 'Joseph Story': {'pres': 'James Madison', 'yrAppt': 1811, 'yrLeft': 1845, 'state': 'MA'}, 'Stanley Reed': {'pres': 'Franklin Roosevelt', 'yrAppt': 1938, 'yrLeft': 1957, 'state': 'KY'}, 'Nathan Clifford': {'pres': 'James Buchanan', 'yrAppt': 1858, 'yrLeft': 1881, 'state': 'ME'}, 'Oliver Ellsworth': {'pres': 'George Washington', 'yrAppt': 1796, 'yrLeft': 1800, 'state': 'CT'}, 'Harlan Stone': {'pres': 'Calvin Coolidge', 'yrAppt': 1925, 'yrLeft': 1946, 'state': 'NY'}, 'John Jay': {'pres': 'George Washington', 'yrAppt': 1789, 'yrLeft': 1795, 'state': 'NY'}, 'John Harlan II': {'pres': 'Dwight Eisenhower', 'yrAppt': 1955, 'yrLeft': 1971, 'state': 'NY'}, 'Oliver Holmes': {'pres': 'Theodore Roosevelt', 'yrAppt': 1902, 'yrLeft': 1932, 'state': 'MA'}, 'David Brewer': {'pres': 'Benjamin Harrison', 'yrAppt': 1889, 'yrLeft': 1910, 'state': 'KS'}, 'Alfred Moore': {'pres': 'John Adams', 'yrAppt': 1799, 'yrLeft': 1804, 'state': 'NC'}, 'Noah Swayne': {'pres': 'Abraham Lincoln', 'yrAppt': 1862, 'yrLeft': 1881, 'state': 'OH'}, 'John Marshall': {'pres': 'John Adams', 'yrAppt': 1801, 'yrLeft': 1835, 'state': 'VA'}, 'Robert Grier': {'pres': 'James Polk', 'yrAppt': 1846, 'yrLeft': 1870, 'state': 'PA'}, 'Tom Clark': {'pres': 'Harry Truman', 'yrAppt': 1949, 'yrLeft': 1967, 'state': 'TX'}, 'John Rutledge': {'pres': 'George Washington', 'yrAppt': 1789, 'yrLeft': 1795, 'state': 'MA'}, 'George Sutherland': {'pres': 'Warren Harding', 'yrAppt': 1922, 'yrLeft': 1938, 'state': 'VT'}, 'William Day': {'pres': 'Theodore Roosevelt', 'yrAppt': 1903, 'yrLeft': 1922, 'state': 'OH'}, 'Harry Blackman': {'pres': 'Richard Nixon', 'yrAppt': 1970, 'yrLeft': 1994, 'state': 'MN'}, 'John Catron': {'pres': 'Martin Van Buren', 'yrAppt': 1837, 'yrLeft': 1865, 'state': 'TN'}, 'Horace Lurton': {'pres': 'William Taft', 'yrAppt': 1910, 'yrLeft': 1914, 'state': 'TN'}, 'Stanley Matthews': {'pres': 'James Garfield', 'yrAppt': 1881, 'yrLeft': 1889, 'state': 'OH'}, 'Philip Barbour': {'pres': 'Andrew Jackson', 'yrAppt': 1836, 'yrLeft': 1841, 'state': 'VA'}, 'Bushrod Washington': {'pres': 'John Adams', 'yrAppt': 1798, 'yrLeft': 1829, 'state': 'VA'}, 'William Rehnquist': {'pres': 'Richard Nixon', 'yrAppt': 1972, 'yrLeft': 2005, 'state': 'AZ'}, 'William Brennan': {'pres': 'Dwight Eisenhower', 'yrAppt': 1956, 'yrLeft': 1990, 'state': 'NJ'}, 'John Harlan': {'pres': 'Rutherford Hayes', 'yrAppt': 1877, 'yrLeft': 1911, 'state': 'KY'}, 'Sherman Minton': {'pres': 'Harry Truman', 'yrAppt': 1949, 'yrLeft': 1956, 'state': 'IN'}, 'Arthur Goldberg': {'pres': 'John Kennedy', 'yrAppt': 1962, 'yrLeft': 1965, 'state': 'IL'}, 'Joseph McKenna': {'pres': 'William McKinley', 'yrAppt': 1898, 'yrLeft': 1925, 'state': 'CA'}, 'George Shiras': {'pres': 'Benjamin Harrison', 'yrAppt': 1892, 'yrLeft': 1903, 'state': 'PA'}, 'Smith Thompson': {'pres': 'James Monroe', 'yrAppt': 1823, 'yrLeft': 1843, 'state': 'NY'}, 'Antonin Scalia': {'pres': 'Ronald Reagan', 'yrAppt': 1986, 'yrLeft': 0, 'state': 'VA'}, 'Anthony Kennedy': {'pres': 'Ronald Reagan', 'yrAppt': 1987, 'yrLeft': 0, 'state': 'CA'}, 'Stephen Field': {'pres': 'Abraham Lincoln', 'yrAppt': 1863, 'yrLeft': 1897, 'state': 'CA'}, 'Lewis Powell': {'pres': 'Richard Nixon', 'yrAppt': 1972, 'yrLeft': 1987, 'state': 'VA'}, 'Gabriel Duval': {'pres': 'James Madison', 'yrAppt': 1811, 'yrLeft': 1836, 'state': 'MD'}, 'Peter Daniel': {'pres': 'Martin Van Buren', 'yrAppt': 1841, 'yrLeft': 1860, 'state': 'VA'}, 'Howell Jackson': {'pres': 'Benjamin Harrison', 'yrAppt': 1893, 'yrLeft': 1895, 'state': 'TN'}, 'William Patterson': {'pres': 'George Washington', 'yrAppt': 1793, 'yrLeft': 1806, 'state': 'NJ'}, 'Horace Gray': {'pres': 'Chester Arthur', 'yrAppt': 1881, 'yrLeft': 1902, 'state': 'MA'}, 'Brockholst Livingston': {'pres': 'Thomas Jefferson', 'yrAppt': 1806, 'yrLeft': 1823, 'state': 'NY'}, 'Joseph Lamar': {'pres': 'William Taft', 'yrAppt': 1911, 'yrLeft': 1916, 'state': 'GA'}, 'Earl Warren': {'pres': 'Dwight Eisenhower', 'yrAppt': 1953, 'yrLeft': 1969, 'state': 'CA'}, 'Elena Kagan': {'pres': 'Barack Obama', 'yrAppt': 2010, 'yrLeft': 0, 'state': 'NY'}, 'William Strong': {'pres': 'Ulysses Grant', 'yrAppt': 1870, 'yrLeft': 1880, 'state': 'PA'}, 'Levi Woodbury': {'pres': 'James Polk', 'yrAppt': 1845, 'yrLeft': 1851, 'state': 'NH'}, 'William Woods': {'pres': 'Rutherford Hayes', 'yrAppt': 1880, 'yrLeft': 1887, 'state': 'CA'}, 'James Wilson': {'pres': 'George Washington', 'yrAppt': 1789, 'yrLeft': 1798, 'state': 'PA'}, 'Ruth Ginsburg': {'pres': 'Bill Clinton', 'yrAppt': 1993, 'yrLeft': 0, 'state': 'NY'}, 'Charles Hughes': {'pres': 'William Taft', 'yrAppt': 1910, 'yrLeft': 1941, 'state': 'NY'}, 'John McLean': {'pres': 'Andrew Jackson', 'yrAppt': 1829, 'yrLeft': 1861, 'state': 'OH'}, 'Warren Burger': {'pres': 'Richard Nixon', 'yrAppt': 1969, 'yrLeft': 1987, 'state': 'VA'}, 'Samuel Blatchford': {'pres': 'Chester Arthur', 'yrAppt': 1882, 'yrLeft': 1893, 'state': 'NY'}, 'Thomas Johnson': {'pres': 'George Washington', 'yrAppt': 1791, 'yrLeft': 1793, 'state': 'MD'}, 'Melville Fuller': {'pres': 'Grover Cleveland', 'yrAppt': 1888, 'yrLeft': 1910, 'state': 'IL'}, 'Fred Vinson': {'pres': 'Harry Truman', 'yrAppt': 1946, 'yrLeft': 1953, 'state': 'KY'}, 'Joseph Bradley': {'pres': 'Ulysses Grant', 'yrAppt': 1879, 'yrLeft': 1892, 'state': 'NJ'}, 'Pierce Butler': {'pres': 'Warren Harding', 'yrAppt': 1922, 'yrLeft': 1939, 'state': 'MN'}, 'William Moody': {'pres': 'Theodore Roosevelt', 'yrAppt': 1906, 'yrLeft': 1910, 'state': 'MA'}, 'James McReynolds': {'pres': 'Woodrow Wilson', 'yrAppt': 1914, 'yrLeft': 1941, 'state': 'TN'}, 'Felix Frankfurter': {'pres': 'Franklin Roosevelt', 'yrAppt': 1939, 'yrLeft': 1962, 'state': 'MA'}, 'Henry Baldwin': {'pres': 'Andrew Jackson', 'yrAppt': 1830, 'yrLeft': 1844, 'state': 'PA'}, "Sandra O'Connor": {'pres': 'Ronald Reagan', 'yrAppt': 1981, 'yrLeft': 2005, 'state': 'AZ'}, 'David Davis': {'pres': 'Abraham Lincoln', 'yrAppt': 1862, 'yrLeft': 1877, 'state': 'IL'}, 'Mahlon Pitney': {'pres': 'William Taft', 'yrAppt': 1912, 'yrLeft': 1922, 'state': 'NJ'}, 'William Cushing': {'pres': 'George Washington', 'yrAppt': 1789, 'yrLeft': 1810, 'state': 'MA'}, 'Byron White': {'pres': 'John Kennedy', 'yrAppt': 1962, 'yrLeft': 1993, 'state': 'CO'}, 'Stephen Breyer': {'pres': 'Bill Clinton', 'yrAppt': 1994, 'yrLeft': 0, 'state': 'MA'}, 'John McKinley': {'pres': 'Martin Van Buren', 'yrAppt': 1837, 'yrLeft': 1852, 'state': 'AL'}, 'Owen Roberts': {'pres': 'Herbert Hoover', 'yrAppt': 1930, 'yrLeft': 1945, 'state': 'PA'}, 'Willis Van Devanter': {'pres': 'William Taft', 'yrAppt': 1910, 'yrLeft': 1937, 'state': 'WY'}, 'Hugo Black': {'pres': 'Franklin Roosevelt', 'yrAppt': 1937, 'yrLeft': 1971, 'state': 'AL'}, 'James Wayne': {'pres': 'Andrew Jackson', 'yrAppt': 1835, 'yrLeft': 1867, 'state': 'GA'}, 'John Stevens': {'pres': 'Gerald Ford', 'yrAppt': 1975, 'yrLeft': 2010, 'state': 'IL'}, 'Edward White': {'pres': 'Grover Cleveland', 'yrAppt': 1894, 'yrLeft': 1921, 'state': 'LA'}, 'David Souter': {'pres': 'George H. W. Bush', 'yrAppt': 1990, 'yrLeft': 2009, 'state': 'NH'}, 'Abe Fortas': {'pres': 'Lyndon Johnson', 'yrAppt': 1965, 'yrLeft': 1969, 'state': 'TN'}, 'John Campbell': {'pres': 'Franklin Pierce', 'yrAppt': 1853, 'yrLeft': 1861, 'state': 'AL'}, 'Charles Whittaker': {'pres': 'Dwight Eisenhower', 'yrAppt': 1957, 'yrLeft': 1962, 'state': 'MO'}, 'Robert Trimble': {'pres': 'John Q. Adams', 'yrAppt': 1826, 'yrLeft': 1828, 'state': 'KY'}, 'Henry Brown': {'pres': 'Benjamin Harrison', 'yrAppt': 1890, 'yrLeft': 1906, 'state': 'MI'}, 'William Taft': {'pres': 'Warren Harding', 'yrAppt': 1921, 'yrLeft': 1930, 'state': 'CT'}, 'Salmon Chase': {'pres': 'Abraham Lincoln', 'yrAppt': 1864, 'yrLeft': 1873, 'state': 'OH'}, 'Samuel Nelson': {'pres': 'John Tyler', 'yrAppt': 1845, 'yrLeft': 1872, 'state': 'NY'}, 'Louis Brandeis': {'pres': 'Woodrow Wilson', 'yrAppt': 1916, 'yrLeft': 1939, 'state': 'MA'}, 'Wiley Rutledge': {'pres': 'Franklin Roosevelt', 'yrAppt': 1943, 'yrLeft': 1949, 'state': 'IA'}, 'John Blair': {'pres': 'George Washington', 'yrAppt': 1789, 'yrLeft': 1796, 'state': 'VA'}, 'Robert Jackson': {'pres': 'Franklin Roosevelt', 'yrAppt': 1941, 'yrLeft': 1954, 'state': 'NY'}, 'Samuel Miller': {'pres': 'Abraham Lincoln', 'yrAppt': 1862, 'yrLeft': 1890, 'state': 'IA'}, 'Harold Burton': {'pres': 'Harry Truman', 'yrAppt': 1945, 'yrLeft': 1958, 'state': 'OH'}, 'Clarence Thomas': {'pres': 'George H. W. Bush', 'yrAppt': 1991, 'yrLeft': 0, 'state': 'GA'}, 'James Iredell': {'pres': 'George Washington', 'yrAppt': 1790, 'yrLeft': 1799, 'state': 'NC'}}
In Exercises 51 through 54, use the file JusticesDict.dat that stores a dictionary contain- ing data about the Supreme Court justices, past and present. Each item of the dictionary has the form “name of justice:data dictionary. The data dictionary for a justice contains their appointing president, the state from which they were appointed, the year appointed, and the year they left the court. (For current justices, the year they left the court is set to 0.) Three items from the dictionary are as follows: Earl Warren' 'pres: Dwight E1senhower', 'yrLeft':1969, 'yrAppt 1953, 'state: CA') Sonia Sotomayor' :'pres: Barack Obama', 'yrLeft' :0, 'yrAppt':2009, 'state' 'NY" Salmon Chase 'yrAppt':1864, 'pres Abraham Lincoln', 'state 'OH', 'yrLeft' :1873) 51. Supreme Court Write a program that requests the name of a president as input and then displays the names and years of the justices appointed by that president. See Fig. 5.36. Enter a president: John Kennedy Enter a state abbreviation: NH 1990 1845 Arthur Goldberg 1962 David Souter Byron White 1962 Levi Woodbury FIGURE 5.36 Possible outcome of Exercise 5 FIGURE 5.37 Possible outcome of Exercise 52. 52. Supreme Court Write a program that requests a state abbreviation as input and then displays the names and years of the justices appointed from that state. See Fig. 5.37 53. Supreme Court Write a program that requests the name of a justice and displays the justice's data. See Fig. 5.38 31 states have produced Justices Enter name of a 1ustice John Roberts Appointed by George W. Bush State: MD Year of appointment: 2005 Currently serving on Supreme Court AL: 3 AZ: 2 CA: 5 CO: 1 CT: 3 Possible outcome of Exercise 53. First part of outcome of Exercise 54. FIGURE 5.38 FIGURE 5.39 Chapter 5 Processing Data 54. Supreme Court Write a program that displays the name of each state (alphabeti- cally) that has produced a Supreme Court justice and the number of justices pro- duced. Fig. 5.39 shows the first six lines displayedExplanation / Answer
I have written the code in the python 3.6 (Installed on my system). it should also work on the python 3.5
Python code:
#!/usr/bin/python3.6
import ast
## reading the data from the file.
with open("JusticesDict.dat", 'r') as contentfile:
content = contentfile.read().strip()
##converting the String into dict
DataDict = ast.literal_eval(content)
##Q51
def appointed_justice():
pres = input("Enter a president : ")
for key,value in DataDict.items():
if value['pres'] == pres:
print(key, value['yrAppt'], " ")
#Q52
def justices_from_state():
state = input("Enter a state abberiviation : ")
for key,value in DataDict.items():
if value['state'] == state:
print(key, value['yrAppt']," ")
#Q53
def justice_detail():
justice = input("Enter a name of justice")
if justice not in DataDict:
print("not found")
return
print("Appointed by",DataDict[justice]['pres'])
print("State", DataDict[justice]['state'])
print("year of appointment", DataDict[justice]['yrAppt'])
if DataDict[justice]['yrLeft'] == 0:
print("Currently serving in the supereme court")
#Q54
def number_of_justice_from_state():
count_jus = {}
for key, value in DataDict.items():
state = value['state']
if state not in count_jus.keys():
count_jus[state] = 0
else :
count_jus[state] +=1
## printing in alphabatical order.
print("States have produced justices : ")
for key in sorted(count_jus.keys()):
print(key, ":", count_jus[key])
appointed_justice()
justices_from_state()
justice_detail()
number_of_justice_from_state()
Output after running this program:
$python3.6 main.py
Enter a president : George Washington
Samuel Chase 1796
Oliver Ellsworth 1796
John Jay 1789
John Rutledge 1789
William Patterson 1793
James Wilson 1789
Thomas Johnson 1791
William Cushing 1789
John Blair 1789
James Iredell 1790
Enter a state abberiviation : NC
Alfred Moore 1799
James Iredell 1790
Enter a name of justiceJohn Rutledge
Appointed by George Washington
State MA
year of appointment 1789
States have produced justices :
AL : 2
AZ : 1
CA : 4
CO : 0
CT : 2
GA : 2
IA : 1
IL : 3
IN : 0
KS : 0
KY : 4
LA : 0
MA : 9
MD : 4
ME : 0
MI : 1
MN : 1
MO : 0
MS : 0
NC : 1
NH : 1
NJ : 4
NY : 15
OH : 8
PA : 5
SC : 1
TN : 5
TX : 0
VA : 7
VT : 0
WY : 0
Please share your feedback and concern.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.