''' DS2000 Spring 2022 Writing and calling functions -- dunkin donuts! ''' import matplotlib.pyplot as plt DD_FILE = "DD_US.csv" LAT_COL = 1 LONG_COL = 0 LANEY_LAT = 42.30537 LANEY_LONG = -71.061 def euclidean(x1, y1, x2, y2): ''' Function: euclidean Parameters: four floats, repping two points in the x/y plane Returns: a float, the euclidean distance between the points ''' x_diff = (x2 - x1) ** 2 y_diff = (y2 - y1) ** 2 distance = x_diff + y_diff distance = distance ** .5 return distance def get_lat_long(dd_row, lat_col, long_col): ''' Function: get_lat_long Parameter: a string, one line from the DD csv file and two ints for lat column and long column Returns: a list of two floats -- the lat/long of a DD location ''' dd = dd_row.split(",") lat = float(dd[lat_col]) long = float(dd[long_col]) lst = [lat, long] return lst def main(): # Keep track of all the lats/longs we've seen of # DD locations so far lats = [] longs = [] # Step one: gather data from the file, # using readline until we hit empty space with open(DD_FILE, "r") as infile: header = infile.readline() while True: dd = infile.readline() if dd == "": break # Step two: compute euclidean distance from my house coords = get_lat_long(dd, LAT_COL, LONG_COL) dist = euclidean(LANEY_LAT, LANEY_LONG, coords[0], coords[1]) lats.append(coords[0]) longs.append(coords[1]) print("Distance from my house to dunks:", dist) # Step three: communicate! plt.scatter(longs, lats, color = "orange") plt.plot(LANEY_LONG, LANEY_LAT, 'D', color = "fuchsia") main()