''' DS2500 Spring 2025 Sample code from class - 1/31/25 On Tuesday, we debugged the code so we know there are no errors On Friday, we wrote unit tests for filter_age and convert_time to make sure we're doing our computations correctly End result is a histogram showing the distribution of marathon finish times for master's runners in the 2023 boston marathon (top 1000) ''' import matplotlib.pyplot as plt from utils import read_csv, lst_to_dct FILENAME = "boston_marathon_2023.csv" BIB_HEADER = "BibNumber" AGE_HEADER = "AgeOnRaceDay" RANK_HEADER = "RankOverall" GENDER_HEADER = "Gender" TIME_HEADER = "OfficialTime" def filter_age(target_age, ages, times): ''' given a target age, a list of ages, and a list of times, filter and return a list of times for runners who are the given age OR OLDER ''' filtered_times = [] for i in range(len(ages)): if ages[i] >= target_age: filtered_times.append(times[i]) return filtered_times def convert_time(str_time): ''' given a string with a time in HH:MM:SS format, compute and return an int, the total number of minutes ''' lst = str_time.split(":") hours = int(lst[0]) mins = int(lst[1]) return hours * 60 + mins def plot_finish_times(times, title): ''' given a list of finish times, create a histogram showing their distribution ''' plt.hist(times, color = "fuchsia") plt.title(title) plt.xlabel("Finish time (minutes)") plt.ylabel("Number of runners") plt.show() def main(): # Gather data - read in the MBTA speed restrictions as a 2d list # and then convert to a dictionary where keys come from the header data = read_csv(FILENAME, skip = 0) dct = lst_to_dct(data) # pull out a list of ages ages = dct[AGE_HEADER] for i in range(len(ages)): ages[i] = int(ages[i]) print(f"Ages of runners in 2023: {ages}") # filter to keep everyone older than me, and convert to int master_times = filter_age(45, ages, dct[TIME_HEADER]) master_times = [convert_time(time) for time in master_times] # plot the distribution of finish times plot_finish_times(master_times, "Boston Marathon 2023 Master's Runners (Top 1000)") if __name__ == "__main__": main()