Command Palette

Search for a command to run...

Hectal
Phase 1: How LLMs Work (for Engineers)
LLM Engineering · Exercise 2 of 5 PreviousNext
Beginner

Cosine similarity

Write cosine(a, b) for two lists of numbers of the same length: the dot product divided by the product of the two lengths (square roots of the sums of squares).

If either vector is all zeros, return 0.0 instead of dividing by zero.

What Check tests

  1. Same direction gives 1
  2. Right angles give 0
  3. Opposite directions give -1
  4. Works with 3 numbers
  5. A zero vector gives 0.0, not an error

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.