The scores dataset is a list of dictionaries, one of whose entries is given below for your reference:
{'SeqNo': 0, 'Name': 'Devika', 'Gender': 'F', 'City': 'Bengaluru',
'Mathematics': 85, 'Physics': 100, 'Chemistry': 79}
You are given the following function, which groups the students by the city they hail from:
def group_by_city(scores_dataset):
cities = dict()
for student in scores_dataset:
name, city = student['Name'], student['City']
if city not in cities:
cities[city] = []
cities[city].append(name)
return cities
Write a function named busy_cities that accepts scores_dataset as argument. It should return a list of cities. Each city in this list has the property that the number of students from this city is greater than or equal to the number of students from every other city in the dataset. Note that several cities can be tied for the maximum. Your function must make use of group_by_city.
Return the cities in the order in which they first appear in the dataset. Include the definition of group_by_city in your answer as well, so that your code runs on its own.
You do not have to accept input from the user or print anything.
Example
busy_cities([{'Name': 'Lalit', 'City': 'Kanpur'},
{'Name': 'Karthik', 'City': 'Chennai'},
{'Name': 'Sapana', 'City': 'Kanpur'}])
-> ['Kanpur']
Implement this function:
def group_by_city(scores_dataset):
Write the function only — the arguments are read for you and the return value is printed automatically.
Arguments arrive as one Python literal per line, in this order: scores_dataset.
Input
[{'SeqNo': 0, 'Name': 'Devika', 'Gender': 'F', 'City': 'Bengaluru', 'Mathematics': 85, 'Physics': 100, 'Chemistry': 79}, {'SeqNo': 1, 'Name': 'Karthik', 'Gender': 'M', 'City': 'Chennai', 'Mathematics': 60, 'Physics': 72, 'Chemistry': 91}, {'SeqNo': 2, 'Name': 'Lalit', 'Gender': 'M', 'City': 'Kanpur', 'Mathematics': 45, 'Physics': 58, 'Chemistry': 66}, {'SeqNo': 3, 'Name': 'Sapana', 'Gender': 'F', 'City': 'Kanpur', 'Mathematics': 92, 'Physics': 88, 'Chemistry': 74}]
Output
['Kanpur']