224k views
5 votes
Functions

The following are the functions you must implement:
no further restrictions on this function besides what is listed on the restrictions page 2 def locateEmergency (hospFile, polFile):
Description: Implement a function that transforms data from 2 files into a dictionary of data to make working with these files a little easier.
Parameters: hospFile is a string, it is the name of the file containing information regarding hospitals and urgent cares in the Fairfax County area. polFile is a string, it is the name of the file containing information regarding police stations in the Fairfax County area.
File Format:
The hospital CSV file will always contain the following columns in this order: TYPE DESCRIPTION JURISDICTION WEB_ADDRESS STREET NUMBER STREET NAME CITY ZIP PHONE
The police CSV file will always contain the following columns in this order: STATION_NAME STATION_ADDRESS
ZIP
PHONE NUMBER
Assumptions:
The file names given by hospFile and polfile will exist and contain at least the header row.
Return Value: A dictionary. The key is zipcode. The value is a 2D list containing all the hospitals, urgent cares, and police stations in that same zipcode. The first element in each sublist will always be one of three strings: 'Urgent Care' 'Hospital' 'Police Station'

User Sonic Lee
by
7.9k points

2 Answers

2 votes

Final answer:

Implement a function that transforms data from two files into a dictionary of data to make working with these files easier.

Step-by-step explanation:

Description
Implement a function that transforms data from 2 files into a dictionary of data to make working with these files a little easier.

  1. Parameters: hospFile is a string, it is the name of the file containing information regarding hospitals and urgent cares in the Fairfax County area. polFile is a string, it is the name of the file containing information regarding police stations in the Fairfax County area.
  2. File Format:The hospital CSV file will always contain the following columns in this order: TYPE DESCRIPTION JURISDICTION WEB_ADDRESS STREET NUMBER STREET NAME CITY ZIP PHONE.The police CSV file will always contain the following columns in this order: STATION_NAME STATION_ADDRESS ZIP PHONE NUMBER
  3. Assumptions: The file names given by hospFile and polfile will exist and contain at least the header row.
  4. Return Value: A dictionary. The key is zipcode. The value is a 2D list containing all the hospitals, urgent cares, and police stations in that same zipcode. The first element in each sublist will always be one of three strings: 'Urgent Care' 'Hospital' 'Police Station'
User Jonalv
by
7.5k points
6 votes

An example of the Python implementation of the locateEmergency function is shown below

python

import csv

def locateEmergency(hospFile, polFile):

# Define the dictionary to store the data

emergency_data = {}

# Process hospital file

with open(hospFile, 'r') as hosp_csv:

hosp_reader = csv.reader(hosp_csv)

next(hosp_reader) # Skip the header row

for row in hosp_reader:

zip_code = row[-1]

emergency_type = row[0]

data = row[1:-1]

if zip_code not in emergency_data:

emergency_data[zip_code] = []

emergency_data[zip_code].append([emergency_type] + data)

# Process police file

with open(polFile, 'r') as pol_csv:

pol_reader = csv.reader(pol_csv)

next(pol_reader) # Skip the header row

for row in pol_reader:

zip_code = row[-1]

emergency_type = 'Police Station'

data = row[:-1]

if zip_code not in emergency_data:

emergency_data[zip_code] = []

emergency_data[zip_code].append([emergency_type] + data)

return emergency_data

# Example usage:

hospitals_file = 'hospitals.csv' # Replace with the actual file name

police_file = 'police_stations.csv' # Replace with the actual file name

result = locateEmergency(hospitals_file, police_file)

print(result)

So, the function above is one that reads the data from both files, organizes it by zip code, and returns a dictionary with zip codes as keys and a 2D list of emergency facilities for each zip code as values.

User Mirtha
by
8.1k points

No related questions found