'''' DS2000 Fall 2024 Sample code from class - looking for Dunks in a given city, maybe also plotting Two functions to write: - turn a string into a list of strings, except lat/long should be floats - look for a given city in a list at position 2 ''' import matplotlib.pyplot as plt FILENAME = "dunks.csv" LONG_POS = 0 LAT_POS = 1 CITY_POS = 2 def process_str(whole_str, long, lat): ''' parameters: a string, two ints (positions) returns: a list of strings, except at the two given positions does: splits the given string into a list of strings, converts the two positions to floats ''' lst = whole_str.split(",") lst[lat] = float(lst[lat]) lst[long] = float(lst[long]) return lst def find_city(lst, city, city_pos): ''' parameters: lst of floats/strings, string for city, int for city_pos returns: boolean does: finds if the given city exists in the list at the given position ''' if city in lst[city_pos]: return True else: return False def main(): # prompt the user for a city city = input("What city to look for Dunks in?\n") # step one - gather data by reading from the file with open(FILENAME, "r") as infile: infile.readline() for line in infile: # turn line into a list, because line is a string right now lst = process_str(line, LONG_POS, LAT_POS) # Is this dunks in the city the user gave us? found = find_city(lst, city, CITY_POS) if found: print(city, "found!") # communication - plot the lat/long plt.plot(lst[LONG_POS], lst[LAT_POS], "x", color = "orange") # more communication, final touches and show the plot plt.title("Dunkin Donuts Locations in the US") plt.xlabel("Longitude") plt.ylabel("Latitude") plt.show() main()