Write a pyhton function to calculate distance between two 3d vectors, include some tests
- User
Write a pyhton function to calculate distance between two 3d vectors, include some tests
Assistant
def calculate_distance(v1, v2): """ Calculate the distance between two 3D vectors.
Args: v1 (list): The first vector. v2 (list): The second vector. Returns: float: The distance between the two vectors. """ # Check if the vectors have the correct dimensions if len(v1) != 3 or len(v2) != 3: raise ValueError("Both vectors must be 3D.") # Calculate the distance using the Euclidean distance formula return ((v1[0] - v2[0])**2 + (v1[1] - v2[1])**2 + (v1[2] - v2[2])**2)**0.5Test cases
Test with correct inputs
v1 = [1, 2, 3] v2 = [4, 5, 6] result = calculate_distance(v1, v2) print(result) # Output: 5.196152422706632
Test with incorrect dimensions
v1 = [1, 2] v2 = [4, 5, 6] try: result = calculate_distance(v1, v2) except ValueError as e: print(e) # Output: Both vectors must be 3D.
Test with zero distance
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.