Team Ai
Apppublic

cactus183/patchbench-dev

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
test_code.py37 linesDownload Raw Back to medium_02
1import math2from solution import magnitude, normalize, cosine_similarity3 4 5def test_magnitude_unit_vector():6    assert abs(magnitude([1, 0, 0]) - 1.0) < 1e-97 8 9def test_magnitude_3_4_5():10    # magnitude of [3, 4] = 511    assert abs(magnitude([3, 4]) - 5.0) < 1e-912 13 14def test_normalize_unit_vector():15    result = normalize([3, 4])16    # should be [0.6, 0.8]17    assert abs(result[0] - 0.6) < 1e-918    assert abs(result[1] - 0.8) < 1e-919 20 21def test_cosine_same_direction():22    # cosine similarity of parallel vectors should be 1.023    sim = cosine_similarity([1, 2, 3], [2, 4, 6])24    assert abs(sim - 1.0) < 1e-625 26 27def test_cosine_orthogonal():28    # cosine similarity of orthogonal vectors should be 0.029    sim = cosine_similarity([1, 0], [0, 1])30    assert abs(sim - 0.0) < 1e-631 32 33def test_cosine_opposite():34    # cosine similarity of opposite vectors should be -1.035    sim = cosine_similarity([1, 2], [-1, -2])36    assert abs(sim - (-1.0)) < 1e-637