Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using python, write code for problem. The file USpresStatesDict.dat is used for

ID: 3849785 • Letter: U

Question

Using python, write code for problem. The file USpresStatesDict.dat is used for this problem and i have uploaded that file to my google drive and shared the link here, since there is no option to upload the file here. Please also put screenshot of your code.

USpresStatesDict.dat

https://drive.google.com/file/d/0B1GZQvpTojdPQi00N3VubWNjWWM/view?usp=sharing

57. U.S. Presidents Use the file UspresstatesDict. dat to obtain an ordered list of the states that were home to three or more presidents. Each state should be followed by the number of presidents from that state and the states should be ordered by the num ber of presidents they produced. See Fig. 5.42.

Explanation / Answer

NOTE: I was not able to download the file from google drive link containing USpresStatesDict.dat because it downloads the file as encrypted. So i have created a csv file with president, state and wrote the script. The file contents are shown in "Execution and output" below. Based on your file format we can change the script easily. Otherwise please share atleast 10 lines in the script so i can modify the script. Logic will not change irrespective of the file format.

Code:
#!/usr/local/bin/python3

def main():
stateDict = {}
# taking filename as input
filename = input('Enter the filename? ')

with open(filename) as fp:
# iterating through each line in file
for line in fp:
# fetching president and state from line
president, state = line.strip().split(',')
# if state already exists in dictionary we will append president to the same state
if state in stateDict:
stateDict[state].append(president)
# state does not exists so creating key in dictionary
else:
stateDict[state] = [president]
  
print('States that produced three or more presidents are:')
for state in stateDict:
# getting length of presidents in state
pres_in_state = len(stateDict[state])
if pres_in_state >= 3:
print(state,': ',pres_in_state)

fp.close()

if __name__=='__main__':
main()


Execution and output:
Input file will look like this.

Unix Terminal> cat states.csv
George Washington,Virginia
John Adams,Massachusetts
Thomas Jefferson,Virginia
John Quincy Adams,Massachusetts
Andrew Jackson,Tennessee
Martin Van Buren,New York
William Henry Harrison,Virginia
John Tyler,Virginia
James K. Polk,Tennessee
Zachary Taylor,Virginia
Millard Fillmore,New York
Franklin Pierce,New Hampshire
James Buchanan,Pennsylvania
Abraham Lincoln,Illinois
Andrew Johnson,Tennessee
Ulysses S. Grant,Illinois
Rutherford B. Hayes,Ohio
Chester A. Arthur,New York
Grover Cleveland,New York
Benjamin Harrison,Indiana
William McKinley,Ohio
Theodore Roosevelt,New York
William Howard Taft,Ohio
Woodrow Wilson,Virginia
Warren G. Harding,Ohio
Calvin Coolidge,Massachusetts
Herbert Hoover,California
Franklin D. Roosevelt,New York
Harry S. Truman,Missouri
Dwight D. Eisenhower,Kansas
John F. Kennedy,Massachusetts
Lyndon B. Johnson,Texas
Richard Nixon,California
Gerald Ford,Michigan
Jimmy Carter,Georgia
Ronald Reagan,California
George H. W. Bush,Texas
Bill Clinton,Arkansas
George W. Bush,Texas
Barack Obama,Illinois
Donald J. Trump,New York
Unix Terminal>

Unix Terminal> python3 states_with_pres.py
Enter the filename? states.csv
States that produced three or more presidents are:
Virginia : 6
Massachusetts : 4
Tennessee : 3
New York : 7
Illinois : 3
Ohio : 4
California : 3
Texas : 3
Unix Terminal>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote