easy +10 pts

Euclidean Distance Matrix

Compute pairwise Euclidean distances between two sets of points using math.sqrt.

Write a function `euclidean_distance_matrix(A, B)` that takes two lists `A` and `B` representing sets of points. Each point is a list or tuple of coordinates. The function returns a list of lists `D` where `D[i][j]` is the Euclidean distance between the i-th point in `A` and the j-th point in `B`. **Details:** - `A` has `m` points and `B` has `p` points. - Each point has `n` coordinates (all points in both lists have the same number of coordinates). - `m, p, n >= 1`. - The returned value must be a list of lists (or list of tuples) of floats, shape `(m, p)`. - The Euclidean distance between point `a = (a1, a2, ..., an)` and point `b = (b1, b2, ..., bn)` is `sqrt((a1-b1)**2 + (a2-b2)**2 + ... + (an-bn)**2)`. - You may use `math.sqrt`; no external libraries are needed. **Implementation:** ```python def euclidean_distance_matrix(A, B): pass ``` Your function will be called with lists of numbers (either int or float). The result should be a list of lists of floats.

Constraints

Inputs: `A` and `B` are lists of points. Each point is a list or tuple of numbers. All points have the same length `n`. `m = len(A)`, `p = len(B)`, `m, p, n >= 1`. Output must be a list of lists of floats with shape `(m, p)`. Complexity: O(m * p * n) time, O(m * p) memory.

Example

```python
>>> euclidean_distance_matrix([[0,0],[1,1]], [[0,1],[1,0]])
[[1.0, 1.0], [1.0, 1.0]]

>>> euclidean_distance_matrix([[1,2,3]], [[4,5,6],[7,8,9]])
[[5.196152422706632, 10.392304845413264]]
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

For each point in A, loop over each point in B and compute the distance.
Compute the squared distance first by summing the squares of coordinate differences.
Use math.sqrt to get the final distance.
Accumulate the results row by row to build the matrix.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.