np.dot multiplies matching elements and adds them, but only for vectors of the same length. Model it, including the error.
What to build
A program that prints the dot product of two vectors.
Requirements
- Print
dot Dfor vectors of the same length. - Otherwise print
error: shapes (N,) and (M,) not aligned, with N and M the two lengths.
Input
Two lines of whole numbers: the vectors u and v.
Output
One line.
Constraints
1 to 1000 numbers per line; |x| ≤ 10⁴.
Example
Input:
1 2 3
4 5 6
Output:
dot 32
Why it works
A dot product pairs each element of one vector with the matching element of the other, so the lengths must be equal; zip would silently drop the extra elements, which is why the check comes first.
Examples
Example 1 · Input
1 2 3 4 5 6
Expected output
dot 32
Example 2 · Input
1 0 0 1
Expected output
dot 0
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Check the lengths before multiplying.
Hint 2
sum(a * b for a, b in zip(u, v)) is the dot product.
Solution
Show one correct solution (try it yourself first)
u = [int(x) for x in input().split()]
v = [int(x) for x in input().split()]
if len(u) != len(v):
print(f"error: shapes ({len(u)},) and ({len(v)},) not aligned")
else:
print("dot", sum(a * b for a, b in zip(u, v)))