Rank documents for a query with TF-IDF:
tf(t, d) = count of t in d / number of tokens in d.idf(t) = ln(N / df(t)) + 1 where N is the number of documents and df the number of documents
containing t. Terms that appear in no document contribute 0.Return the ids of the top k documents with score > 0, highest first, ties broken by id ascending.
Implement top_k. Input: docs: dict[str, str], query: str, k: int. Returns: list[str].
Only d1 contains 'pod' and 'restarts' exactly; d2 has 'pods' and 'restarting', which are different tokens.
def top_k(docs: dict[str, str], query: str, k: int) -> list[str]:
return sorted(docs)[:k]
Run executes the public tests. Submit grades against public and hidden tests.
Rank documents for a query with TF-IDF:
tf(t, d) = count of t in d / number of tokens in d.idf(t) = ln(N / df(t)) + 1 where N is the number of documents and df the number of documents
containing t. Terms that appear in no document contribute 0.Return the ids of the top k documents with score > 0, highest first, ties broken by id ascending.
Implement top_k. Input: docs: dict[str, str], query: str, k: int. Returns: list[str].
Only d1 contains 'pod' and 'restarts' exactly; d2 has 'pods' and 'restarting', which are different tokens.
def top_k(docs: dict[str, str], query: str, k: int) -> list[str]:
return sorted(docs)[:k]
Run executes the public tests. Submit grades against public and hidden tests.