In: Computer Science
Deliverables
There is one deliverable for this assignment
Make sure the script obeys all the rules in the Script Requirements page.
Specification
The file has entries like the following
Barnstable,Barnstable,1 Bourne,Barnstable,5 Brewster,Barnstable,9 ...
This script should create a dictionary where the county is the key and then total number of cases for the country is the value.
The script should print the name of the county with the highest number of cases along with the total cases.
The script must have 3 functions
This script 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.
highest_cases
This function must have the following header
def highest_cases(county_cases):
The function
Script 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 test code to work, you must copy cities_counties_cases.txt to your machine.
To do this use FileZilla to copy the file cities_counties_cases.txt f rom /home/ghoffman/course_files/it117_files into the directory that holds your hw4.py script.
Run the script entering cities_counties_cases.txt when prompted.
You should see
$ ./hw4.py File name: cities_counties_cases.txt Worcester 455
Suggestions
Write this program in a step-by-step fashion using the technique of incremental development.
In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.
CODE -
# Function to create a file object for reading on the file whose name is given by the parameter
# and return the file object if successful in creating file object.
def open_file_read(filename):
# Opening the file
cases_file = open(filename)
# Returning the file object if file is opened successfully
if cases_file:
return cases_file
else:
# Printing error message and returning None if file can't be opened.
print("File cannot be Opened.")
return None
# Function to read file and create and return dictionary containing county names and cases
def cases_dictionary_create(file):
# Creating empty dictionary
cases_dict = {}
# Reading file line by line
for line in file:
# Seperating city name, county name, and cases from the file which is seperated by comma
line_list = line.split(",")
# Adding county name and case to the dictionary if county name is not already in the dictionary
if line_list[1] not in cases_dict:
cases_dict[line_list[1]] = int(line_list[2].strip())
# Adding no. of cases to the cases of the corresponding county if county name is already in the dictionary
else:
cases_dict[line_list[1]] += int(line_list[2].strip())
# Returning the dictionary
return cases_dict
# Function to find and return county name and cases with highest no. of cases.
def highest_cases(county_cases):
# Initializing max_county to empty string
max_county = ""
# Initializing max_cases to 0
max_cases = 0
# Iterating over each county in the dictionary
for county in county_cases:
cases = county_cases[county]
# Checking if no. of cases for current county is greater than max_cases.
# If yes, assigning current county name to max_county and no. of cases of current county to max_cases
if cases > max_cases:
max_county = county
max_cases = cases
# Returning name of county and no. of cases of county with max no. of cases
return max_county, max_cases
# Testing the script.
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)
SCREENSHOTS -
If you have any doubt regarding the solution, then do comment.
Do upvote.