''' DS2000 Spring 2022 Sample code from class - practice with dictionaries! Input file has geographical data for NEU students, and has the form: state, amount state, amount state, amount we want to transform the input file into a dictionary where key = state, and value = num students from that state ''' import csv NEU_FILE = "neu_geo.csv" def csv_to_dict(filename): ''' Function: csv_to_dict Parameter: filename, a string Returns: a dictinoary where key - state (str), value - num students (int) ''' data = {} with open(filename, "r") as infile: csvfile = csv.reader(infile, delimiter = ",") next(csvfile) for row in csvfile: data[row[0]] = int(row[1]) return data def max_state(count_dct): ''' Function: max_state parameter: dictionary where key - string, value - num of occurrences returns: a tuple (key, value) for the highest value in the dictionary ''' max_key = "" max_value = -1 for key, value in count_dct.items(): if value > max_value: max_value = value max_key = key return (max_key, max_value) def main(): # Step one: gather data from the csv file states = csv_to_dict(NEU_FILE) # step two - computation, # find the state with the most students state, students = max_state(states) # step three - communciation print(state, "has the most students at NEU, with", students) # # Step two: computation # state = input("Which state to find out?\n") # num_students = states[state] # # Step three: communicate! print out # print("There are", num_students, "students from", state) main()