def count_beans(beans): '''Return a dictionary mapping colors to the number of beans of that color.''' color_counts = {} for color in beans: color_counts[color] = color_counts.get(color, 0) + 1 return color_counts def most_common_bean(beans): '''Return a 2-tuple of the color and count of the most common bean. Assume no ties.''' sorted_colors = sorted(beans, key=beans.get) # sort keys by value most_common_color = sorted_colors[-1] # get most common color largest_count = beans[most_common_color] return (most_common_color, largest_count) def most_common_bean_v2(beans): '''Return a 2-tuple of the color and count of the most common bean. Assume no ties.''' largest_count = max(beans.values()) for color in beans: if beans[color] == largest_count: return (color, largest_count) def most_common_beans(beans): '''Return a list of 2-tuple of the color and count of the most common beans.''' result_list = [] largest_count = max(beans.values()) for color in beans: if beans[color] == largest_count: result_list.append((color, largest_count)) return result_list ############## Project Gutenberg examples ############# import re def count_words(filename): '''Return a dictionary of words to the number of times that word appears in filename.''' word_counts = {} file = open(filename, errors='ignore') for line in file: # loop over the lines of the file words = re.findall('[a-zA-Z]+', line) # grab all the words from the line for word in words: # tally each word word = word.lower() # ignore capitals word_counts[word] = word_counts.get(word, 0) + 1 file.close() return word_counts def top_words(filename): '''Return the ten most frequently used words in filename as (word, count) tuples.''' word_map = count_words(filename) sorted_keys = sorted(word_map, key=word_map.get) first_ten = sorted_keys[:20] # the ten most frequently occurring words top_list = [] for word in first_ten: top_list.append((word, word_map[word])) return top_list def unique_words(filename, all_filenames): '''Returns a set of words that appear in filename but NOT in any of all_filenames.''' unique_words = set(count_words(filename)) for other_filename in all_filenames: if other_filename != filename: other_words = set(count_words(other_filename)) unique_words -= other_words return unique_words