All problems
Response Retrieval by Cosine
Problem
Return the id of the response with the highest cosine similarity to the query. responses is a list of [id, vector].
Ties are broken alphabetically by id, and a zero-norm query or vector scores 0.0. Return an empty string for an empty response list.
Hint
Sort by id before scanning so strict improvements break ties alphabetically.
Test cases
case 1
in: [[1,0],[["r1",[1,0]],["r2",[0,1]],["r3",[1,1]]]]
expected: "r1"
case 2
in: [[1,0],[["b",[0,1]],["a",[0,1]]]]
expected: "a"
case 3
in: [[0,0],[["a",[1,0]]]]
expected: "a"
case 4
in: [[],[["a",[1,0]],["b",[1,0]]]]
expected: "a"
case 5
in: [[1,0],[]]
expected: ""
Starter codePython
import math
def response_retrieval_cosine(query, responses):
# Your code here
pass5 test cases · run your code against them in the editor