Command Palette

Search for a command to run...

Hectal
Phase 2: Embeddings in Depth
RAG and Vector Search · Exercise 1 of 5Next
Beginner

Length, normalise and check

Write length(v): the square root of the sum of the squares of the numbers in v.

Write normalise(v): a new list scaled to length 1. A vector of all zeros cannot be scaled, so return a list of zeros of the same size instead of failing.

Write is_unit(v, tolerance=0.001): True when the length of v is within tolerance of 1.

What Check tests

  1. length is the Euclidean length
  2. normalise scales to length 1
  3. normalise does not change its input
  4. A zero vector gives zeros, not an error
  5. is_unit tells unit from non-unit vectors

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.