← run

py-06-numpy-distances

1.000
3/3 tests· math

Proposed solution

```python
import numpy as np

def pairwise_distances(points: np.ndarray) -> np.ndarray:
    # Compute squared differences using broadcasting
    # points[:, np.newaxis, :] creates shape (n, 1, d)
    # points[np.newaxis, :, :] creates shape (1, n, d)
    # Subtracting gives shape (n, n, d)
    diff = points[:, np.newaxis, :] - points[np.newaxis, :, :]
    
    # Square the differences and sum along the last axis (d dimensions)
    squared_distances = np.sum(diff**2, axis=2)
    
    # Take square root to get Euclidean distances
    return np.sqrt(squared_distances)
```

Test output (stdout)

...                                                                      [100%]
3 passed in 0.05s
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.