Implement Dot Product
Implement Dot Product
The dot product of two equal-length 1-D arrays x and y of length n is defined algebraically as:
x⋅y=i=1∑nxiyi=x1y1+x2y2+⋯+xnynand geometrically as:
x⋅y=∥x∥⋅∥y∥⋅cos(θ)where ∥x∥ and ∥y∥ are the magnitudes of x and y, and θ is the angle between them.
Return the dot product as a scalar float.
Examples
Input: x = [1,2,3], y = [4,5,6]
Output: 32.0
1×4 + 2×5 + 3×6 = 4 + 10 + 18 = 32
Input: x = [1,0], y = [0,1]
Output: 0.0
Orthogonal vectors (perpendicular)
Input: x = [-1,2], y = [3,-1]
Output: -5.0
(-1)×3 + 2×(-1) = -3 + (-2) = -5
Hint 1
Convert inputs to NumPy arrays first, then use vectorized operations.
Hint 2
NumPy has a built-in function for this: np.dot(x, y).
Requirements
- Must work for lists or NumPy arrays
- Must return a float
- Must be vectorized (no Python element loops)
- Must handle 1D arrays only
- Must raise ValueError for mismatched lengths
Constraints
- Time limit: 200 ms, Memory: 64 MB
- NumPy only (no sklearn, scipy)
Log in to take notes on this problem
Accepts: array
Accepts: array
Implement Dot Product
Implement Dot Product
The dot product of two equal-length 1-D arrays x and y of length n is defined algebraically as:
x⋅y=i=1∑nxiyi=x1y1+x2y2+⋯+xnynand geometrically as:
x⋅y=∥x∥⋅∥y∥⋅cos(θ)where ∥x∥ and ∥y∥ are the magnitudes of x and y, and θ is the angle between them.
Return the dot product as a scalar float.
Examples
Input: x = [1,2,3], y = [4,5,6]
Output: 32.0
1×4 + 2×5 + 3×6 = 4 + 10 + 18 = 32
Input: x = [1,0], y = [0,1]
Output: 0.0
Orthogonal vectors (perpendicular)
Input: x = [-1,2], y = [3,-1]
Output: -5.0
(-1)×3 + 2×(-1) = -3 + (-2) = -5
Hint 1
Convert inputs to NumPy arrays first, then use vectorized operations.
Hint 2
NumPy has a built-in function for this: np.dot(x, y).
Requirements
- Must work for lists or NumPy arrays
- Must return a float
- Must be vectorized (no Python element loops)
- Must handle 1D arrays only
- Must raise ValueError for mismatched lengths
Constraints
- Time limit: 200 ms, Memory: 64 MB
- NumPy only (no sklearn, scipy)
Log in to take notes on this problem
Accepts: array
Accepts: array