''' DS2000 Spring 2023 Sample code from class - read in data from a csv fileand... - Find out who ran the most mileage in Jan - Use a for loop to iterate over the file - use split(",") to turn a stiring into a list - Skip or separately use the header (dates) - plot lists ''' import matplotlib.pyplot as plt RUNNER_FILE = "runner_data.csv" NAME_POS = 0 def main(): # Initialize variables needed for max runner's info max_mileage = -1 max_runner = "" # Gather data - read in each runner's data from the file with open(RUNNER_FILE, "r") as infile: # Save the header (dates) dates = infile.readline() dates = dates.split(",") dates = [int(date) for date in dates] # Iterate over every line in the file, and process # one runner's data each time for line in infile: # Turn the current runner's data into a list lst = line.split(",") # Save the runner's name as a string name = lst[NAME_POS] # Save the runner's mileage as a list, use slicing runner_data = lst[NAME_POS + 1:] # Convert the runner data to floats # Using list comprehension runner_data = [float(num) for num in runner_data] # Sanioty check print, should be floats now # print("Runner's mileage (floats?):", runner_data) # Find out if the current runner is the new max current_mileage = sum(runner_data) print(name, ":", current_mileage) if current_mileage >= max_mileage: max_mileage = current_mileage max_runner = name # Plot data for the current runner plt.scatter(dates, runner_data, label = name, marker = "x") # Communicate - who ran the most and how much? # This is outside the for loop, and outside the with/open print(max_runner, "ran the most in January, with...", max_mileage, "miles!") plt.legend() main()