You are given a dictionary datasets where each key is a dataset name (for example, a year or a region) and each value is a list of baby names ordered by popularity. The name at index 0 has rank 1, the name at index 1 has rank 2, and so on.
Given a target name name, return a list of [dataset_name, rank] pairs, one for every dataset in which name appears. The returned list must be sorted by rank in ascending order. If the name does not appear in any dataset, return an empty list.
Function Signature
def search_name(datasets: dict[str, list[str]], name: str) -> list[list]:
Rules
-
A name matches only when it is exactly equal to
name
, character for character. The comparison is case-sensitive, and a name that only contains
name
as a substring does not match.
-
Rank is the 1-based position in the list: a match at index
i
has rank
i + 1
.
-
Each pair is a two-element list: the dataset name as a string, then the rank as an integer.
-
Sort the pairs by rank ascending. When two datasets have the same rank, order them by dataset name ascending using plain string comparison (Python's
<
on
str
).
-
A dataset whose list is empty, or that does not contain
name
, contributes no pair.
Constraints
-
1 <= number of datasets <= 10^3
-
0 <= length of each names list <= 10^4
-
1 <= len(name), len(dataset_name) <= 50
-
All strings consist of printable ASCII characters.
-
Within a single dataset, names are unique.
Examples
Input: datasets = {"2020": ["Olivia", "Liam", "Emma", "Noah"], "2021": ["Liam", "Olivia", "Noah", "Emma"], "2022": ["Sophia", "Mia"]}, name = "Emma"
Output: [["2020", 3], ["2021", 4]]
"Emma" is at index 2 in 2020 (rank 3) and at index 3 in 2021 (rank 4). It is not in the 2022 list, so 2022 contributes nothing.
Input: datasets = {"US": ["Aiden", "Bella"], "UK": ["Charlie", "Diana"]}, name = "Zoe"
Output: []
"Zoe" does not appear in any dataset, so the result is an empty list.
Input: datasets = {"west": ["Ava", "Mia"], "east": ["Leo", "Mia"], "north": ["Mia"], "south": ["mia", "Miah"]}, name = "Mia"
Output: [["north", 1], ["east", 2], ["west", 2]]
"Mia" has rank 1 in north and rank 2 in both east and west; the tie at rank 2 is broken by dataset name, and "east" sorts before "west". The south list has no match, because "mia" differs in case and "Miah" is a different name.