You are working on a search engine team and need to implement a search result scoring function. Given a query string, a list of documents, and an integer array weights of length 3, rank the documents by their relevance score in descending order.
The relevance score of a document is the weighted sum weights[0] * exact_matches + weights[1] * unique_matches + weights[2] * coverage, where:
-
exact_matches
is the number of times the entire
query
string appears as a substring of the document.
-
unique_matches
is the number of distinct words from the query that appear at least once in the document. A word is a maximal sequence of non-space characters.
-
coverage
is 100 if every distinct word from the query appears in the document, and 0 otherwise.
Return the indices of the documents sorted by score in descending order. If two documents have the same score, the one with the smaller original index comes first.
Function Signature
def rank_documents(query: str, documents: list[str], weights: list[int]) -> list[int]:
Rules
-
exact_matches
counts non-overlapping occurrences of
query
exactly as given (spaces included), scanning the document left to right. This is the count Python's
str.count
returns.
-
Exact matches ignore word boundaries: the query
"fast car"
occurs once in
"breakfast cart"
.
-
Query words are matched as whole words:
"car"
does not match the document word
"cars"
.
-
A word repeated in the query counts once toward
unique_matches
and
coverage
.
-
If the query contains no words (only spaces),
unique_matches
and
coverage
are both 0.
-
Indices are 0-based. Ties on score go to the smaller index, so the output order is unique.
Constraints
-
1 <= len(documents) <= 10^4
-
1 <= len(query) <= 100
-
1 <= len(documents[i]) <= 1000
-
len(weights) == 3
-
0 <= weights[i] <= 100
-
All strings consist of lowercase English letters and spaces.
Examples
Input: query = "fast car", documents = ["a fast car is a nice car", "slow car", "fast car fast car", "i like cars and bikes", "fast"], weights = [10, 5, 1]
Output: [2, 0, 1, 4, 3]
The scores are 120, 5, 130, 0 and 5: document 2 has two exact matches plus both words, document 0 has one exact match plus both words, and documents 1 and 4 each contain one query word, so they tie and index 1 goes first. Document 3 scores 0 because "cars" is a different word from "car".
Input: query = "hello world", documents = ["hello world", "world hello", "hello", "world", "hello world hello world"], weights = [3, 2, 1]
Output: [4, 0, 1, 2, 3]
The scores are 107, 104, 2, 2 and 110. Document 1 has both words in the other order, so it gets coverage but no exact match, and documents 2 and 3 tie at 2.
Input: query = "ab ab", documents = ["ab ab ab", "ab abxab ab", "xab abx"], weights = [10, 5, 1]
Output: [1, 0, 2]
Counts do not overlap, so document 0 has one exact match (the second candidate would reuse its middle "ab") and scores 115, while document 1 has two and scores 125. Document 2 contains the phrase inside "xab abx" for 10 points but has no whole word "ab".