# Write a function call most_common that # accepts the file name to be read. # The file contains courses students will take spring 2019. # The function then returns the most common course # (be careful, the problem asks for the course, not the number # of students who will enroll in the course) def most_common(filename): course_dict = {} infile = open(filename, 'r') list_of_lines = infile.read().split('\n') for line in list_of_lines: columns = line.split(',') # skip column 0 since it is not course name for i in range(1, len(columns)): # by default, python will look in keys of dict if columns[i] in course_dict: # similar to checking in course_dict.keys() course_dict[columns[i]] += 1 # increment value associate with key = columns[i] else: course_dict[columns[i]] = 1 # add a key-value pair to dict where key is columns[i] # done with file, don't forget to close it infile.close() # Iterate over a course_dict and find course (key) with the highest value # Don't use max(course_dict) since it will return # max of course_dict's key, not the value. max_enroll = 0 common_course = '' for k,v in course_dict.items(): if max_enroll < v: max_enroll = v common_course = k return common_course print('most_common ---', most_common('course-s19.csv')) # Create a second (modified) version of the # most_common function that returns a dictionary, # where the key is course and the value is # the number of students who will take that course def most_common2(filename): course_dict = {} infile = open(filename, 'r') list_of_lines = infile.read().split('\n') for line in list_of_lines: columns = line.split(',') # skip column 0 since it is not course name for i in range(1, len(columns)): # by default, python will look in keys of dict if columns[i] in course_dict: # similar to checking in course_dict.keys() course_dict[columns[i]] += 1 # increment value associate with key = columns[i] else: course_dict[columns[i]] = 1 # add a key-value pair to dict where key is columns[i] # done with file, don't forget to close it infile.close() return course_dict print('most_common2 ---', most_common2('course-s19.csv')) # Create another (modified) version of the # most_common2 function that, print a dictionary (instead of return) # in the following format: # 5 students will take CS1 # 2 students will take CS2 # ... # (don't worry about the order, just focus on using key and value # and format the output) def most_common3(filename): course_dict = {} infile = open(filename, 'r') list_of_lines = infile.read().split('\n') for line in list_of_lines: columns = line.split(',') # skip column 0 since it is not course name for i in range(1, len(columns)): # by default, python will look in keys of dict if columns[i] in course_dict: # similar to checking in course_dict.keys() course_dict[columns[i]] += 1 # increment value associate with key = columns[i] else: course_dict[columns[i]] = 1 # add a key-value pair to dict where key is columns[i] # done with file, don't forget to close it infile.close() for k,v in course_dict.items(): print(v, 'students will take', k) # print('most_common3 ---', most_common3('course-s19.csv')) # since most_common3 doesn't return anything (i.e., None), print will show None print('most_common3 ---') most_common3('course-s19.csv')