Command Palette

Search for a command to run...

Hectal
Phase 3: Vector Search: Exact, IVF, HNSW and Smaller Vectors
RAG and Vector Search · Exercise 3 of 6 PreviousNext
Intermediate

One round of k-means

points and centroids are lists of (x, y) pairs. Write assign(points, centroids) that returns, for each point, the index of the nearest centroid by squared distance. If two centroids are equally near, use the lower index.

Write update(points, labels, centroids) that returns the new centroids as a list of (x, y) tuples: each is the average of the points with that label. A centroid with no points keeps its old position (as a tuple).

What Check tests

  1. Points go to the nearest centroid
  2. A tie goes to the lower index
  3. Centroids move to the average
  4. An empty cluster keeps its centroid
  5. The results are tuples

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.