logo CodeStepByStep logo

get_majority_last_name

Language/Type: Python dict collections

Write a function named get_majority_last_name that accepts as its parameter a dictionary from strings to strings the keys of the dictionary represent first names and the values represent last names. If there is a single common last name that is present in more than half of the key/value pairs in the dictionary passed in (a "majority" last name), your function should return that last name. If there is no majority last name, your function should return the string "?" .

For example, if the dictionary contains the following key/value pairs, the majority last name is "Smith" because it occurs 5 times, which is more than half of the nine pairs in the dictionary. Therefore your function would return "Smith".

{'Hal': 'Perkins', 'Mark': 'Smith', 'Mike': 'Smith', 'Stuart': 'Reges', 'David': 'Smith',
 'Jean': 'Reges', 'Geneva': 'Smith', 'Amie': 'Smith', 'Bruce': 'Reges'}

The following dictionaries don't have any majority last name because no last name occurs strictly greater than half the time. Therefore when passed either of the dictionaries below, your function would return "?" .

{'Marty': 'Stepp', 'Mehran': 'Sahami', 'Keith': 'Schwarz', 'Cynthia': 'Lee', 'Yogurt': 'Schwarz',
 'Tywin': 'Lannister', 'Rob': 'Stark', 'Sansa': 'Stark', 'Tyrion': 'Lannister'}

If the dictionary that contains only one key/value pair, that pair's value is the majority last name. An empty dictionary does not have any majority last name.

Constraints: You may declare at most one auxiliary collection to help you solve this problem. Do not modify the map that is passed in to your function as a parameter.

Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.