Dot Product: Multiply Pairs, Then Sum

Linear AlgebraEasy+10 XP+10

Dot Product: Multiply Pairs, Then Sum

The dot product multiplies two vectors position by position and adds up the results:

1a · b = Σ a[i] * b[i]

Task: write dot_product(vector_a, vector_b) returning the result, rounded to 4 decimal places.

  • The vectors always have the same length.
  • Two empty vectors give 0.

Almost everything is built on this. A matrix multiply is a grid of dot products, a neuron's pre-activation is one against its weights, and attention scores every query against every key the same way. It's also a similarity measure: perpendicular vectors give exactly zero, which is what the second test case shows.

0.6×0.4—
-0.2×0.3—
0.5×0.7—
score0.00

Multiply matching positions and add. That is a dot product — the same operation a convolution filter performs.

Examples

vector_a = [1.0, 2.0, 3.0]
vector_b = [4.0, 5.0, 6.0]
returns 32.0
vector_a = [1.0, 0.0]
vector_b = [0.0, 1.0]
returns 0.0

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run