-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsolution.py
More file actions
102 lines (77 loc) · 3.86 KB
/
solution.py
File metadata and controls
102 lines (77 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# write your Python code here according to the instructions written in the instructions
## import the csv module
import csv
def get_csv_data(filepath):
"""
Opens the file at filepath, reads the data using the csv module's DictReader,
converts that data to a regular list and returns that list.
:param filepath: The file path of the CSV data file to open
:returns: A list of dictionaries, where each dictionary represents one row from the file
"""
## place your code here to complete this method according to the instructions above
def remove_rows_with_blank_neighborhood_fields(data):
"""
Removes any rows with blank 'nta' or 'nta_code' fields from the data set
:param data: The data, as a list of dictionaries
:returns: The modified data, as a list of dictionaries
"""
## place your code here to complete this method according to the instructions above
def remove_non_free_rows(data):
"""
Removes any rows with anything other than 'Free' in the 'type' field.
:param data: The data, as a list of dictionaries
:returns: The modified data, as a list of dictionaries
"""
## place your code here to complete this method according to the instructions above
def make_location_title_case(data):
"""
Puts the data in the 'location' field into Title Case, where the first letter of each word is capitalized.
:param data: The data, as a list of dictionaries
:returns: The modified data, as a list of dictionaries
"""
## place your code here to complete this method according to the instructions above
def fix_provider(data, old_provider, new_provider):
"""
Swaps out the old provider name with the updated new provider name for any rows that match.
:param data: The data, as a list of dictionaries
:param old_provider: The old provider name to remove, e.g. 'SpotOnNetworks'
:param new_provider: The new domain to replace the old_domain with, e.g. 'Spot On Networks'
:returns: The modified data, as a list of dictionaries
"""
## place your code here to complete this method according to the instructions above
def save_csv_data(data, filepath):
"""
Saves the data into the specified file. Include the field headers as the first row.
:param data: The data, as a list of dictionaries
:param filepath: The file path of the CSV data file to save to
"""
## place your code here to complete this method according to the instructions above
def get_number_free_hotspots(filepath, neighborhood):
"""
Calculates the average cost per impression of all records in the data set.
:param filepath: The file path of the CSV data file to open
:param neighborhood: The neighborhood within which to count free wifi hotspots
:returns: The number of free wifi hotspots within the indicated neighborhood
"""
## place your code here to complete this method according to the instructions above
#################################################
## Do not modify the code below this line ##
## except to comment out any function calls ##
## that you do not wish to test at the moment ##
#################################################
def main():
## use the functions defined above to complete munging of the data file
# get the data from the file
data = get_csv_data('data/wifi.csv')
# munge it
data = remove_rows_with_blank_neighborhood_fields(data)
data = remove_non_free_rows(data)
data = make_location_title_case(data)
data = fix_provider(data, 'SpotOnNetworks', 'Spot On Networks')
# dave to the new csv file
save_csv_data(data, 'data/users_clean.csv')
# print the average cost per impression from the data in the file
num = get_number_free_hotspots('data/wifi_clean.csv', 'Fort Greene')
print('There are ' + str(num) + ' free Wi-Fi hotspots in Fort Greene, Brooklyn.')
if __name__ == "__main__":
main()