''' DS2000 Spring 2023 Sample code from class - March 3rd 1. given a state, how many students are from there? 2. which state has the most students at NEU? ''' import csv STATE_FILE = "neu_geography.csv" def read_csv(filename): ''' Function: read_csv Parameter: filename, a string Returns: dictionary of key, value pairs Does: iterate over the file, first in every row beomes key, second thing becomes value (int) ''' data = {} with open(filename, "r") as infile: csvfile = csv.reader(infile) next(csvfile) for row in csvfile: data[row[0]] = int(row[1]) return data def find_max(dct): ''' Function: find_max Parameter: dictionary where keys can be anything, values are non-neg numbers Return: max value and its key Does: iterate over the dictionary, find highest value and return that with its key ''' max_val = -1 max_key = "" for key, value in dct.items(): if value > max_val: max_val = value max_key = key return max_key, max_val def main(): # Gather data - read from the csv file into a dictionary dct = read_csv(STATE_FILE) # 1. given a state, how many people are from there? state = input("Which state?\n") num_students = 0 if state in dct: num_students = dct[state] # 2. which state has the most students here? max_state, max_num = find_max(dct) # Communication - print out results print("There are", num_students, "from", state) print("State with the most is.....", max_state, "with....", max_num, "students") print("Total students this year:", sum(dct.values())) main()