-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcityreader.py
More file actions
92 lines (73 loc) · 3.09 KB
/
cityreader.py
File metadata and controls
92 lines (73 loc) · 3.09 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
import csv
# Create a class to hold a city location. Call the class "City". It should have
# fields for name, lat and lon (representing latitude and longitude).
class City():
def __init__(self, name, lat, lon):
self.name = name
self.lat = lat
self.lon = lon
def __repr__(self):
return f"City({self.name}, {self.lat}, {self.lon})"
# We have a collection of US cities with population over 750,000 stored in the
# file "cities.csv". (CSV stands for "comma-separated values".)
#
# In the body of the `cityreader` function, use Python's built-in "csv" module
# to read this file so that each record is imported into a City instance. Then
# return the list with all the City instances from the function.
# Google "python 3 csv" for references and use your Google-fu for other examples.
#
# Store the instances in the "cities" list, below.
#
# Note that the first line of the CSV is header that describes the fields--this
# should not be loaded into a City object.
cities = []
def cityreader(cities=[]):
# TODO Implement the functionality to read from the 'cities.csv' file
# For each city record, create a new City instance and add it to the
# `cities` list
with open('cities.csv', 'r') as file:
csv_file = csv.DictReader(file)
for row in csv_file:
cities.append(City(row['city'], float(
row['lat']), float(row['lng'])))
return cities
cityreader(cities)
# Print the list of cities (name, lat, lon), 1 record per line.
for c in cities:
print(f"{c.name}, {c.lat}, {c.lon}")
# STRETCH GOAL!
#
# Allow the user to input two points, each specified by latitude and longitude.
# These points form the corners of a lat/lon square. Pass these latitude and
# longitude values as parameters to the `cityreader_stretch` function, along
# with the `cities` list that holds all the City instances from the `cityreader`
# function. This function should output all the cities that fall within the
# coordinate square.
#
# Be aware that the user could specify either a lower-left/upper-right pair of
# coordinates, or an upper-left/lower-right pair of coordinates. Hint: normalize
# the input data so that it's always one or the other, then search for cities.
# In the example below, inputting 32, -120 first and then 45, -100 should not
# change the results of what the `cityreader_stretch` function returns.
#
# Example I/O:
#
# Enter lat1,lon1: 45,-100
# Enter lat2,lon2: 32,-120
# Albuquerque: (35.1055,-106.6476)
# Riverside: (33.9382,-117.3949)
# San Diego: (32.8312,-117.1225)
# Los Angeles: (34.114,-118.4068)
# Las Vegas: (36.2288,-115.2603)
# Denver: (39.7621,-104.8759)
# Phoenix: (33.5722,-112.0891)
# Tucson: (32.1558,-110.8777)
# Salt Lake City: (40.7774,-111.9301)
# TODO Get latitude and longitude values from the user
def cityreader_stretch(lat1, lon1, lat2, lon2, cities=[]):
# within will hold the cities that fall within the specified region
within = []
# TODO Ensure that the lat and lon valuse are all floats
# Go through each city and check to see if it falls within
# the specified coordinates.
return within