easy +10 pts

Dot Product of Vectors

Compute the dot product of two numeric vectors (lists of numbers).

Write a function `dot_product(a, b)` that takes two lists of numbers `a` and `b` of equal length and returns their dot product. The dot product is the sum of the products of corresponding elements: `a[0]*b[0] + a[1]*b[1] + ... + a[n-1]*b[n-1]`. You may assume the inputs are lists of numeric values (int or float) and that they have the same length. The result should be a number (int or float).

Constraints

Input lists will be non-empty and of equal length. Elements are ints or floats. Complexity should be O(n) time and O(1) extra space.

Example

>>> dot_product([1, 2, 3], [4, 5, 6])
32
>>> dot_product([-1, 2], [3, -4])
-11
>>> dot_product([0.5, 1.5], [2, -2])
-2.0
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a loop to pair elements from both lists.
Accumulate the sum of products in a variable.
You can also use zip and sum for a concise solution.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.