Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

s last_names_by_age 5 Main Page ProblemsSolve a Problem least common Favorite Sh

ID: 3699100 • Letter: S

Question

s last_names_by_age 5 Main Page ProblemsSolve a Problem least common Favorite Show Header Language/Type: Author: Python dict collections Marty Stepp (on 2017/07/08) Write a function least.common that accepts a dictionary whose keys are strings and whose values are integers as a parameter and returns the integer value that occurs the fewest times in the dictionary. For example, if a dictionary named m contains ('Alyssa': 22, 'Char 25, 'Dan': 25, 'Jeff': 28, 'Kasey' 28, 'Kim': 28, 'Mogran': 25, 'Ryan': 25, 'Stef': 22), the call of least.common (m) returns 22. If there is a tie, return the smaller integer value. If the map is empty, throw a ValueError Type your Python solution code here: 4 This is a function problem. Write a Python function as described. Do not write a complete program; just the function(s) above. Sound F/X Highlighting Submit 6 Need help? If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor. If something seems wrong with the site (errors slow performance, incorrect problems/tests, etc.), please contact us

Explanation / Answer

def least_common(d): if len(d) == 0: raise ValueError() nums = list(d.values()) counts = {} for num in nums: if num not in counts: counts[num] = 0 counts[num] += 1 result = None for num, count in counts.items(): if result is None or count