Question

It's python. Please don't forget to print alpha-sorted states. Not random. Thank you // USPresdients.txt KY  ...

It's python. Please don't forget to print alpha-sorted states. Not random. Thank you

//

USPresdients.txt

KY   Abraham_Lincoln
SC   Andrew_Jackson
NC   Andrew_Johnson
HI   Barack_Obama
OH   Benjamin_Harrison
AR   Bill_Clinton
VT   Calvin_Coolidge
VT   Chester_A_Arthur
NY   Donald_Trump
TX   Dwight_D_Eisenhower
NY   Franklin_D_Roosevelt
NH   Franklin_Pierce
MA   George_H_W_Bush
CT   George_W_Bush
VA   George_Washington
NE   Gerald_R_Ford
NJ   Grover_Cleveland
MO   Harry_S_Truman
IA   Herbert_Hoover
OH   James_A_Garfield
PA   James_Buchanan
NC   James_K_Polk
VA   James_Madison
VA   James_Monroe
GA   Jimmy_Carter
MA   John_Adams
MA   John_F_Kennedy
MA   John_Quincy_Adams
VA   John_Tyler
TX   Lyndon_B_Johnson
NY   Martin_Van_Buren
NY   Millard_Fillmore
CA   Richard_M_Nixon
IL   Ronald_Reagan
OH   Rutherford_B_Hayes
NY   Theodore_Roosevelt
VA   Thomas_Jefferson
OH   Ulysses_S_Grant
OH   Warren_G_Harding
VA   William_Henry_Harrison
OH   William_Howard_Taft
OH   William_McKinley
VA   Woodrow_Wilson
VA   Zachary_Taylor

//

Build a second dictionary from the USPresidents.txt file described in the previous exercise. Each key will again be a state abbreviation, however, the value will be the count of presidents from that state.

Create a set of the ten most populous US states (CA, TX, FL, NY, IL, PA, OH, GA, NC, MI).

Then create a new set that represents a set of populous US states that have had presidents born in them (you should be able to do this with one line of code).

Print a count of this new set along with an alpha-sorted listing of these states and how many presidents have been born in them.

Example output:

8 of the 10 high population states have had presidents born in them:
CA 1
GA 1
IL 1
NC 2 
And so on...

And this is what i've done

import sys
import os
file = "C:/Users/USPresidents.txt"

USPDict=dict()
value=1

with open(file) as fp:
for line in fp:
s=line.split()
if s[0] in USPDict:
USPDict[s[0]]+=1
else:
USPDict[s[0]] =value
States={"CA","TX","FL","NY","IL","PA","OH","GA","NC","MI"}
PopulorStates=set()
sorted(PopulorStates)

for val in States:

if(val in USPDict):
PopulorStates.add(val)
print("\n",len(PopulorStates)," of the 10 high population states have had presidents born in them:")

for val in PopulorStates:
if(val in USPDict):
print(val," ",USPDict[val])

0 0
Add a comment Improve this question Transcribed image text
Answer #1

modify the file path accordingly

please drop a comment if there's a problem

#-------------------OUTPUT-----------------------

#----------------------------------------------------

file = "USPresdients.txt"

USPDict = dict()

value = 1

with open(file) as fp:

    for line in fp:

        s = line.split()

        if s[0] in USPDict:

            USPDict[s[0]] += 1

        else:

            USPDict[s[0]] = value

States = {"CA", "TX", "FL", "NY", "IL", "PA", "OH", "GA", "NC", "MI"}

PopulorStates = set()

for val in States:

    if (val in USPDict):

        PopulorStates.add(val)

print("\n", len(PopulorStates),

" of the 10 high population states have had presidents born in them:")

# list to carry the sorted tuples of state name and number

l=[]

#iterate over the PopulorStates

for val in PopulorStates:

    #adds a tuples of the form (name, total) to the list by fetching number from USPDict

    l=l+[(val,USPDict[val])]

        # print(val, " ", USPDict[val])

#using a simple bubble sort to sort the "l" array

n = len(l)

for i in range(n):

    for j in range(0, n-i-1):

    # Swap if 1'st index of the element found is greater than the next element

        if l[j][1] > l[j+1][1]:

            l[j], l[j+1] = l[j+1], l[j]

        #else checks if if the number of presidents is same

        elif l[j][1] == l[j+1][1]:

            #if same then checks if the state name is in albetical order

            if l[j][0] > l[j+1][0]:

                l[j], l[j+1] = l[j+1], l[j]

#prints the list

for i in l:

    print(i[0], i[1])

#----------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
It's python. Please don't forget to print alpha-sorted states. Not random. Thank you // USPresdients.txt KY  ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 2: How do the average credit scores of people living in various cities in the...

    Question 2: How do the average credit scores of people living in various cities in the US differ? The file Credit Score Data of 143 American cities is provided in Canvas. Construct a histogram Create a Five Summary Report Calculate Mean, Variance and Standard Deviation   What conclusion can you reach concerning the average credit scores of people living in different American cities? City State Average Credit Score Detroit Mi 743 New York NY 762 Minneapolis MN 787 Hartford CT 774...

  • CPS 276 DOC02-- Assignment 02 Cost of Living Calculator Your client maintains job search website. As...

    CPS 276 DOC02-- Assignment 02 Cost of Living Calculator Your client maintains job search website. As a service on their website, they need an application that compares costs of living between different metropolitan areas. Client will pay $780 for a complete application. Background Information: A person living in an area with a high cost of living will need higher income to maintain the same standard of living as someone living in an area with a low cost of living. For...

  • CT 60, 894 NY 1,029,888 MO 245,425 MS 92,831 CO 251,595 ME 60,894 PA 573,815 NE...

    CT 60, 894 NY 1,029,888 MO 245,425 MS 92,831 CO 251,595 ME 60,894 PA 573,815 NE 80,432 NC 370,883 ID 64,514 MA 380, 277 IL 574,429 ND 36,863 SC 176,365 MT 45,359 NH 70,699 IN 252,150 SD 38,950 TN 246,025 UT 119, 240 RI 47.731 MI 407,475 AL 165, 716 VA 384,479 WY 25,707 VT 31,284 OH 477, 536 AR 102,755 WV 65,138 AK 37,053 DE 42.655 WI 246,818 FL 876,422 AZ 257,505 CA 1,858,421 DC 44,361 IA 122,914 GA...

  • TravelAir.com samples domestic airline flights to explore the relationship between airfare and distance. The service would...

    TravelAir.com samples domestic airline flights to explore the relationship between airfare and distance. The service would like to know if there is a correlation between airfare and flight distance. If there is a correlation, what percentage of the variation in airfare is accounted for by distance? How much does each additional mile add to the fare? The data follow. Distance Fare 636 $109 2,395 252 2,176 221 605 151 403 138 1,258 209 254 627 259 264 2,342 215 177...

  • and MT NE NV ND NH NJ NM NY NC OH OK OR PA RI SC...

    and MT NE NV ND NH NJ NM NY NC OH OK OR PA RI SC SD TN TX UT VA VT WA WV WI WY EM 95813624 7262 1430278 3 22896 03171 12111 1165380 2111211 1.0160 72221 8 tat-AL AK AZ CA CO CN DE DC FL GA HI ID IL IN IA KA KY LA ME MD MA MI MN MS MO ONEC EDA-NSO git M w w M w w E E E E E W W...

  • With the multiple linear regression equation in (2), what will be the alumni-giving rate with the...

    With the multiple linear regression equation in (2), what will be the alumni-giving rate with the graduation rate as 85%, 60% of classes with fewer than 20 students, and student-faculty ratio as 12? (20%). State Graduation Rate 85 79 93 85 75 Alumni Giving Rate 25 % of Classes Student-Faculty Under 20 Ratio 39 13 68 8 60 8 33 40 65 3 10 46 28 67 72 52 8 31 89 90 45 69 72 12 7 13 10...

  • An article reported the estimated percentage of households with only wireless phone service (no land line)...

    An article reported the estimated percentage of households with only wireless phone service (no land line) for the 50 U.S. states and the District of Columbia. In the accompanying data table, each state was also classified into one of three geographical regions—West (W), Middle states (M), and East (E). Wireless % Region State Wireless % Region State 14.9 M AL 10.2 W MT 12.7 W AK 23.2 M NE 19.9 W AZ 10.8 W NV 23.6 M AR 16.9 M...

  • Business Statistics

    Alumni donations are an important source of revenue for college and universities. If administrators could determine the factors that could lead to increases in thepercentage of alumni who make a donation, they might be able to implement policies that could lead to increased revenues. Research shows that students who are moresatisfied with their contact with teachers are more likely to graduate. As a result, one might suspect that smaller class sizes and lower student-faculty ratios mightlead to a higher percentage...

  • I State United CEO Total Age of Compensation Industry CompanyCity oF Industry ConyBirth of States...

    I State United CEO Total Age of Compensation Industry CompanyCity oF Industry ConyBirth of StatesCEO Birth Region F57 490,001.00 Financial Rochester Passaic 498,500.00 Financi TrustmarkAberdeen 500,000.00 Financi GP 523,040.00 Financial First 523,560.00 Financia SFFed 535,817.00 Financial Michigan Brooklyn 536,930.00 548,34100 Financia Syunovus Augusta 574,745.00 Financial CullentFros San AntoiTX 575,211.00 Financial CSF 53 Financial United Columbus NC Miami Belleville Charlotte Leavenworth KS FL Financial Magna 611,963.00 Financia First 615,841.00 Financia BBT 617,054.00 Financial California NY 627,048.00 Financial Premier 650,443.00 Financial First...

  • 1. Calculate the mean and standard deviation for each variable using formulas or functions. 2. Calculate...

    1. Calculate the mean and standard deviation for each variable using formulas or functions. 2. Calculate descriptive statistics for each variable using the Analysis Toolpak. 3. Calculate the coefficient of variation for each variable. What general interpretation can you make from these values? 4. Calculate the correlation between Revenue and Employees using a formula. Calculate the coefficient of determination. 5. Create a correlation matrix for the eight numerical variables. Note any relationship of interest Rank COMPANY NAME City State Revenue...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT