Cosine Similarity Between Two Vectors
Kestrel Media describes a viewer by a row of scores, one per genre, built from what they watch. To recommend a show it first has to say how alike two viewers are, and the measure it uses is cosine similarity — the standard score for how closely two rows point in the same direction, ignoring how big the numbers in them are. A viewer who watches twice as much as another but splits it across genres in the same proportions counts as identical. The score runs from 1.00 for two viewers pointing the same way, through 0.00 for two with nothing in common, down to -1.00 for two pointing exactly opposite.
Task: Print the cosine similarity of the two viewers.
Input
Three lines. The first holds an integer n, the number of genres. The second holds viewer A's n scores separated by single spaces. The third holds viewer B's n scores, in the same genre order. Neither viewer's row is all zeros.
Output
One line holding the similarity, signed, written with exactly two decimal places.
Example:
Input:
2
1 0
1 0
Output:
1.00
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in