Question

Attention:- Your code should follow all the below instruction. Specification The file has entries like the...

Attention:- Your code should follow all the below instruction.

Specification

The file has entries like the following

Barnstable,Barnstable,1

Bourne,Barnstable,5

Brewster,Barnstable,9

...

This code should create a dictionary where the county is the key and then total number of cases for the country is the value.

The code should print the name of the county with the highest number of cases along with the total cases.

The code must only use below 3 functions

  • open_file_read
  • cases_dictionary_create
  • highest_cases

This code must read in a text file with case numbers for all cities in Massachusetts along with their counties.

open_file_read

This function must have the following header

def open_file_read(filename):

It must try to create a file object for reading on the file whose name is given by the parameter filename.

If it is succesfull in creating the file object it should return the object.

If it is cannot create the object it should print an error message and return None.

cases_dictionary_create

This function must have the following header

def cases_dictionary_create(file):

This function reads a file and creates a dictionary where the keys are counties and the values are total cases.

  • The function returns this dictionary.

highest_cases

This function must have the following header

def highest_cases(county_cases):

The function

for this assignment

Open an a text editor and create the file hw4.py.

You can use the editor built into IDLE or a program like Sublime.

Test Code

Your hw4.py file must contain the following test code at the bottom of the file

filename = input("File name: ")

file = open_file_read(filename)

if file:

        cases = cases_dictionary_create(file)

        max_county, max_cases = highest_cases(cases)

        print(max_county,max_cases)

For this code to work, you must copy below data to cities_counties_cases.txt file to your machine.

Barnstable,Barnstable,1

Bourne,Barnstable,5

Brewster,Barnstable,9

Chatham,Barnstable,2

Dennis,Barnstable,15

Eastham,Barnstable,2

Falmouth,Barnstable,9

Harwich,Barnstable,12

Mashpee,Barnstable,3

Orleans,Barnstable,7

Provincetown,Barnstable,4

Sandwich,Barnstable,4

Truro,Barnstable,2

Wellfleet,Barnstable,14

Yarmouth,Barnstable,7

Adams,Berkshire,9

Alford,Berkshire,14

Becket,Berkshire,1

Cheshire,Berkshire,11

Clarksburg,Berkshire,0

Dalton,Berkshire,3

Egremont,Berkshire,7

Florida,Berkshire,11

Great Barrington,Berkshire,7

Hancock,Berkshire,10

Hinsdale,Berkshire,8

Lanesborough,Berkshire,3

Lee,Berkshire,10

Lenox,Berkshire,10

Monterey,Berkshire,4

Mount Washington,Berkshire,13

New Ashford,Berkshire,11

New Marlborough,Berkshire,15

North Adams,Berkshire,13

Otis,Berkshire,5

Peru,Berkshire,14

Pittsfield,Berkshire,1

Richmond,Berkshire,15

Sandisfield,Berkshire,9

Savoy,Berkshire,15

Sheffield,Berkshire,3

Stockbridge,Berkshire,12

Tyringham,Berkshire,5

Washington,Berkshire,7

West Stockbridge,Berkshire,11

Williamstown,Berkshire,14

Windsor,Berkshire,11

Acushnet,Bristol,11

Attleboro,Bristol,2

Berkley,Bristol,14

Dartmouth,Bristol,8

Dighton,Bristol,4

Easton,Bristol,0

Fairhaven,Bristol,12

Fall River,Bristol,7

Freetown,Bristol,0

Mansfield,Bristol,12

New Bedford,Bristol,13

North Attleborough,Bristol,6

Norton,Bristol,5

Raynham,Bristol,3

Rehoboth,Bristol,6

Seekonk,Bristol,8

Somerset,Bristol,10

Swansea,Bristol,5

Tauntom,Bristol,8

Westport,Bristol,4

Aquinnah,Dukes,4

Chilmark,Dukes,10

Edgartown,Dukes,9

Gosnold,Dukes,9

Oak Bluffs,Dukes,13

Tisbury,Dukes,5

West Tisbury,Dukes,6

Amesbury,Essex,12

Andover,Essex,5

Beverly,Essex,12

Boxford,Essex,15

Danvers,Essex,12

Essex,Essex,5

Georgetown,Essex,15

Gloucester,Essex,12

Groveland,Essex,1

Hamilton,Essex,7

Haverhill,Essex,8

Ipswich,Essex,5

Lawrence,Essex,12

Lynn,Essex,6

Lynnfield,Essex,10

Manchester by the sea,Essex,4

Marblehead,Essex,4

Merrimac,Essex,12

Methuen,Essex,12

Middleton,Essex,14

Nahant,Essex,12

Newbury,Essex,6

Newburyport,Essex,4

North Andover,Essex,10

Peabody,Essex,6

Rockport,Essex,3

Rowley,Essex,6

Salem,Essex,9

Salisbury,Essex,10

Saugus,Essex,13

Swampscott,Essex,13

Topsfield,Essex,7

Wenham,Essex,10

West Newbury,Essex,8

Ashfield,Franklin,14

Bernardston,Franklin,9

Buckland,Franklin,10

Charlemont,Franklin,5

Colrain,Franklin,2

Conway,Franklin,4

Deerfield,Franklin,15

Erving,Franklin,8

Gill,Franklin,2

Greenfield,Franklin,3

Hawley,Franklin,3

Heath,Franklin,13

Leverett,Franklin,0

Leyden,Franklin,6

Monroe,Franklin,13

Montague,Franklin,13

New Salem,Franklin,3

Northfield,Franklin,4

Orange,Franklin,2

Rowe,Franklin,6

Shelburne,Franklin,3

Shutesbury,Franklin,8

Sunderland,Franklin,13

Warwick,Franklin,6

Wendell,Franklin,11

Whately,Franklin,4

Agawam,Hampden,8

Blandford,Hampden,4

Brimfield,Hampden,14

Chester,Hampden,14

Chicopee,Hampden,2

East Longmeadow,Hampden,15

Granville,Hampden,13

Hampden,Hampden,2

Holland,Hampden,9

Holyoke,Hampden,10

Longmeadow,Hampden,5

Ludlow,Hampden,9

Monson,Hampden,4

Montgomery,Hampden,0

Palmer,Hampden,15

Russell,Hampden,15

Southwick,Hampden,1

Springfield,Hampden,13

Tolland,Hampden,5

Wales,Hampden,11

West Springfield,Hampden,7

Westfield,Hampden,14

Wilbraham,Hampden,9

Amherst,Hampshire,13

Belchertown,Hampshire,6

Chesterfield,Hampshire,0

Cummington,Hampshire,13

Easthampton,Hampshire,15

Goshen,Hampshire,4

Granby,Hampshire,5

Hadley,Hampshire,4

Hatfield,Hampshire,2

Huntington,Hampshire,13

Middlefield,Hampshire,7

Northampton,Hampshire,4

Pelham,Hampshire,7

Plainfield,Hampshire,10

South Hadley,Hampshire,4

Southampton,Hampshire,8

Ware,Hampshire,12

Westhampton,Hampshire,0

Williamsburg,Hampshire,15

Worthington,Hampshire,8

Acton,Middlesex,12

Arlington,Middlesex,12

Ashby,Middlesex,6

Ashland,Middlesex,4

Ayer,Middlesex,5

Bedford,Middlesex,6

Belmont,Middlesex,6

Billerica,Middlesex,6

Boxborough,Middlesex,0

Burlington,Middlesex,9

Cambridge,Middlesex,9

Carlisle,Middlesex,1

Chelmsford,Middlesex,15

Concord,Middlesex,0

Dracut,Middlesex,9

Dunstable,Middlesex,9

Everett,Middlesex,5

Framingham,Middlesex,10

Groton,Middlesex,15

Holliston,Middlesex,11

Hopkinton,Middlesex,6

Hudson,Middlesex,8

Lexington,Middlesex,11

Lincoln,Middlesex,12

Littleton,Middlesex,1

Lowell,Middlesex,7

Malden,Middlesex,2

Marlborough,Middlesex,12

Maynard,Middlesex,1

Medford,Middlesex,2

Melrose,Middlesex,14

Natick,Middlesex,8

Newton,Middlesex,0

North Reading,Middlesex,15

Pepperell,Middlesex,13

Reading,Middlesex,8

Sherborn,Middlesex,8

Shirley,Middlesex,6

Somerville,Middlesex,12

Stoneham,Middlesex,6

Stow,Middlesex,11

Sudbury,Middlesex,7

Tewksbury,Middlesex,0

Townsend,Middlesex,10

Tyngsborough,Middlesex,14

Wakefield,Middlesex,2

Wlatham,Middlesex,0

Watertown,Middlesex,14

Wayland,Middlesex,15

Westford,Middlesex,8

Weston,Middlesex,13

Wilmington,Middlesex,5

Winchester,Middlesex,12

Woburn,Middlesex,14

Nantucket,Nantucket,12

Avon,Norfolk,12

Bellingham,Norfolk,6

Braintree,Norfolk,3

Brookline,Norfolk,0

Canton,Norfolk,12

Cohasset,Norfolk,7

Dedham*,Norfolk,2

Dover,Norfolk,4

Foxborough,Norfolk,0

Franklin,Norfolk,10

Holbrook,Norfolk,1

Medfield,Norfolk,13

Medway,Norfolk,12

Millis,Norfolk,14

Milton,Norfolk,15

Needham,Norfolk,9

Norfolk,Norfolk,14

Norwood,Norfolk,14

Plainville,Norfolk,3

Quincy,Norfolk,4

Randolph,Norfolk,10

Sharon,Norfolk,15

Stoughton,Norfolk,6

Walpole,Norfolk,5

Wellesley,Norfolk,13

Westwood,Norfolk,10

Weymouth,Norfolk,2

Wrentham,Norfolk,9

Abington,Plymouth,4

Bridgewater,Plymouth,2

Brockton,Plymouth,5

Carver,Plymouth,12

Duxbury,Plymouth,7

East Bridgewater,Plymouth,3

Halifax,Plymouth,11

Hanover,Plymouth,12

Hanson,Plymouth,11

Hingham,Plymouth,12

Hull,Plymouth,3

Kingston,Plymouth,5

Lakeville,Plymouth,10

Marion,Plymouth,14

Marshfield,Plymouth,4

Mattapoisett,Plymouth,2

Middleborough,Plymouth,2

Norwell,Plymouth,10

Pembroke,Plymouth,5

Plymouth*,Plymouth,2

Plympton,Plymouth,8

Rochester,Plymouth,3

Rockland,Plymouth,2

Scituate,Plymouth,10

Wareham,Plymouth,0

West Bridgewater,Plymouth,3

Whitman,Plymouth,10

Boston,Suffolk,14

Chelsea,Suffolk,2

Revere,Suffolk,11

Winthrop,Suffolk,13

Ashburnham,Worcester,4

Athol,Worcester,4

Auburn,Worcester,11

Barre,Worcester,12

Berlin,Worcester,11

Blackstone,Worcester,9

Bolton,Worcester,4

Boylston,Worcester,1

Brookfield,Worcester,1

Charlton,Worcester,0

Clinton,Worcester,4

Douglas,Worcester,0

Dudley,Worcester,11

East Brookfield,Worcester,3

Fitchburg,Worcester,7

Gardner,Worcester,6

Grafton,Worcester,5

Hardwick,Worcester,11

Harvard,Worcester,15

Holden,Worcester,13

Hopedale,Worcester,15

Hubbardston,Worcester,9

Lancaster,Worcester,11

Leicester,Worcester,3

Leominster,Worcester,15

Lunenburg,Worcester,13

Mendon,Worcester,10

Milford,Worcester,11

Millbury,Worcester,3

Millville,Worcester,7

New Braintree,Worcester,10

North Brookfield,Worcester,6

Northborough,Worcester,2

Northbridge,Worcester,8

Oakham,Worcester,6

Oxford,Worcester,14

Paxton,Worcester,15

Petersham,Worcester,2

Phillipston,Worcester,15

Princeton,Worcester,1

Royalston,Worcester,15

Rutland,Worcester,8

Shrewsbury,Worcester,1

Southborough,Worcester,7

Southbridge,Worcester,1

Spencer,Worcester,10

Sterling,Worcester,5

Sturbridge,Worcester,15

Sutton,Worcester,12

Templeton,Worcester,3

Upton,Worcester,13

Uxbridge,Worcester,12

Warren,Worcester,5

Webster,Worcester,7

West Boylston,Worcester,2

West Brookfield,Worcester,3

Westborough,Worcester,2

Westminster,Worcester,12

Winchendon,Worcester,10

Worcester,Worcester,4

Txt file data end here

Run the code entering cities_counties_cases.txt when prompted.

You should see

$ ./hw4.py

After compile the code you should get the below result

File name: cities_counties_cases.txt

Worcester 455

Homework Answers

Answer #1

'''
Python version : 3.6
Python program to determine the county with the highest number of cases
where data is read from the file
'''

def open_file_read(filename):
   '''
   It must try to create a file object for reading on the file
   whose name is given by the parameter filename.
   If it is succesfull in creating the file object it should return the object.
   If it is cannot create the object it should print an error message and return None.
   '''
  
   try:
       file = open(filename) # open the file
       return file # file opened succesfully
   except IOError:
       # file opening failed
       print('Unable to open file: %s' %filename)
       return None
      
def cases_dictionary_create(file):
   '''
   This function reads a file and creates a dictionary
   where the keys are counties and the values are total cases.
   The function returns this dictionary.  
   '''
  
   # create an empty dictionary
   county_cases = {}
   # read a line from file
   line = file.readline()
   # loop to read the file till the end, line by line
   while line:
       # strip removes the leading and trailing spaces from line
       # split is used to split the string using comma(,) as the delimiter into a list of strings
       data = line.strip().split(',')
       # check if county is present in dictionary
       if data[1] in county_cases:
           # int is used to convert string to integer
           county_cases[data[1]] += int(data[2]) # add the number of cases of this record to dictionary
       else:   # county is not present in dictionary
           # create a new record in dictionary where county is the key and number of cases for this record as value
           county_cases[data[1]] = int(data[2])     
       line = file.readline() # read the next line from file
      
   return county_cases

def highest_cases(county_cases):
   '''
   This function returns the county name and number of cases
   of the county with highest number of cases
   '''
  
   # initialize max_county to None and max_cases to 0
   max_county = None
   max_cases = 0
   # loop over the keys in the dictionary
   for county in county_cases:
       # if max_county is not set or number of cases for county > max_cases
       if max_county == None or county_cases[county] > max_cases:
           # update max_county to county and max_cases to number of cases for this county
           max_county = county
           max_cases = county_cases[county]
          
   return max_county, max_cases

  
# input the filename  
filename = input("File name: ")
file = open_file_read(filename) # open the file
if file: # file open succesfull
   cases = cases_dictionary_create(file) # get the dictionary of county and cases
   max_county, max_cases = highest_cases(cases) # get the county with maximum number of cases
   print(max_county,max_cases) # display the result
  
#end of program  

Code Screenshot:

Output:

Input file: (using the above file)

Output:

Know the answer?
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
Similar Questions
I need a breakdown to perform in excel for numbers 7,8,9. I am unsure of how...
I need a breakdown to perform in excel for numbers 7,8,9. I am unsure of how I calculate the times. heres the data set and the questions. Calculate the probability that a flight will depart early or on time. Calculate the probability that a flight will arrive late. Calculate the probability that a flight departs late or arrives early. DEP_Delay ARR_Delay -4 0 -3 -3 0 -5 -7 -1 8 3 -1 -5 3 8 11 6 -6 0 -5...
A study of reading comprehension in children compared three methods of instruction. The three methods of...
A study of reading comprehension in children compared three methods of instruction. The three methods of instruction are called Basal, DRTA, and Strategies. Basal is the traditional method of teaching, while DRTA and Strategies are two innovative methods based on similar theoretical considerations. The READING data set includes three response variables that the new methods were designed to improve. Analyze these variables using ANOVA methods. Be sure to include multiple comparisons or contrasts as needed. Write a report summarizing your...
A study of reading comprehension in children compared three methods of instruction. The three methods of...
A study of reading comprehension in children compared three methods of instruction. The three methods of instruction are called Basal, DRTA, and Strategies. Basal is the traditional method of teaching, while DRTA and Strategies are two innovative methods based on similar theoretical considerations. The READING data set includes three response variables that the new methods were designed to improve. Analyze these variables using ANOVA methods. Be sure to include multiple comparisons or contrasts as needed. Write a report summarizing your...
Compute each of the following probabilities. Label each problem clearly and show all your work. Use...
Compute each of the following probabilities. Label each problem clearly and show all your work. Use the numbers you computed in earlier parts of the project based on the class data set. Problem 1: Suppose all of the Skittles in the class data set are combined into one large bowl and you are going to randomly select one Skittle. (a) What is the probability that you select a green Skittle? (4 points) (b) What is the probability that you select...
A study of reading comprehension in children compared three methods of instruction. The three methods of...
A study of reading comprehension in children compared three methods of instruction. The three methods of instruction are called Basal, DRTA, and Strategies. As is common in such studies, two pretest variables were measured before any instruction was given. One purpose of the pretest was to see if the three groups of children were similar in their comprehension skills prior to the start of the study. The READING data set described in the Data Appendix gives two pretest measures that...
Dep Delay Arr Delay q1 -13 -9 q3 1 3 IQR 14 12 stdev 9.211846 9.660933...
Dep Delay Arr Delay q1 -13 -9 q3 1 3 IQR 14 12 stdev 9.211846 9.660933 negative 291 zero 39 positive 170 total (should be 500) 500 all data is for arrival data. please help me explain these can someone help me answer these questions and tell me how to put them in excel.thanx in advance 1.    Define a random variable (X) so that your chosen data set represents values of X. 2.    Is your chosen random variable discrete or...
Convert the Ch01_SalesSummary file to a database table in MS Access (steps: drop unnecessary titles row...
Convert the Ch01_SalesSummary file to a database table in MS Access (steps: drop unnecessary titles row and import data into Access using “External Data” Excel tab in Access). Sort the new Access database table in ascending order by any one column. Table Waitstaff Member# #Guests Hour Food Beverage Total 29 WH 0 1 8 0 4 5 28 MM 101 4 8 0 4 5 28 MM 0 2 8 5 8 15 5 LR 0 2 8 5 9...
6. Using the class data (see Blackboard file - "Class Survey Data - PSS-10 and GAS")...
6. Using the class data (see Blackboard file - "Class Survey Data - PSS-10 and GAS") calculate the correlation between the GAS – Goal disengagement and the GAS – Goal reengagement scores. Write the results in a statistical statement. 7. Using the class data calculate the correlation between the GAS – Goal disengagement and the PSS-10 scores. Write the results in a statistical statement. 8. Using the class data calculate the correlation between the GAS – Goal reengagement scores and...
Bag Blue Orange Green Yellow Red Brown Total Number of Candies 1 7 23 9 11...
Bag Blue Orange Green Yellow Red Brown Total Number of Candies 1 7 23 9 11 4 6 60 2 16 16 4 7 9 4 56 3 14 14 3 10 5 11 57 4 13 14 7 8 8 6 56 5 18 12 7 10 5 7 59 6 12 9 11 9 8 8 57 7 15 16 6 6 6 8 57 8 15 17 8 4 6 7 57 9 12 14 10 5...
Electrobat, a battery manufacturer, is investigating how storage temperature affects the performance of one of its...
Electrobat, a battery manufacturer, is investigating how storage temperature affects the performance of one of its popular deep-cell battery models used in recreational vehicles. Samples of 30 fully charged batteries were subjected to a light load under each of four different storage temperature levels. The hours until deep discharge (meaning ≤ 20% of charge remaining) were measured. The data is shown in the accompanying table. 0 degrees F (hrs to discharge) 30 degrees F (hrs to discharge) 60 degrees F...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT