Dot product and angle

Given two vectors with whole-number entries, print their dot product, the angle between them, and how they relate.

Input

Two lines, each with the same count (2 to 10) of whole numbers. Neither vector is all zeros.

Output

Three lines:

  • dot D
  • angle A, in degrees to 1 decimal place
  • relation R, where R is same direction, opposite, perpendicular or other

Examples

Example 1 · Input
3 4
4 3
Expected output
dot 24
angle 16.3
relation other
Example 2 · Input
1 2
-2 1
Expected output
dot 0
angle 90.0
relation perpendicular

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

The dot product adds the products of matching numbers.

Hint 2

cos θ = dot ÷ (|u| × |v|). Clamp it into [−1, 1] before acos: rounding can push it a hair outside.

Hint 3

Compare whole numbers for the relation: dot² = |u|²·|v|² means the vectors are parallel.

Solution

Show one correct solution (try it yourself first)
from math import acos, degrees, sqrt

u = [int(x) for x in input().split()]
v = [int(x) for x in input().split()]
dot = sum(a * b for a, b in zip(u, v))
uu = sum(a * a for a in u)
vv = sum(b * b for b in v)
cos = max(-1.0, min(1.0, dot / sqrt(uu * vv)))
print("dot", dot)
print(f"angle {degrees(acos(cos)):.1f}")
if dot == 0:
    relation = "perpendicular"
elif dot * dot == uu * vv:
    relation = "same direction" if dot > 0 else "opposite"
else:
    relation = "other"
print("relation", relation)

Lesson: Vectors and the dot product →