Immutable 2D Vector With Dunder Operators & Hashing

Difficulty: Easy · Unit 5: OOP, Iterators & Complexity · Featured in: CodeTantra Lab Pattern

Implement an immutable `Vector2D(x, y)` class supporting vector addition (`+`), subtraction (`-`), scalar multiplication (`v * k` and `k * v`), dot product (`v @ u`), equality, and set deduplication. Given N vector commands, output the final resulting vector `(x, y)` and the count of distinct intermediate positions visited (including `(0, 0)`).

Input & Output Format

Input: Line 1: integer N. Next N lines: `<op> <arg1> [arg2]` where op is `ADD dx dy`, `SUB dx dy`, or `SCALE k`.

Output: Print `<x> <y> <distinct_positions>`.

Sample 1

4
ADD 2 3
SCALE 2
SUB 4 6
ADD 2 3
2 3 3

Path: (0,0) -> (2,3) -> (4,6) -> (0,0) -> (2,3). Distinct positions: {(0,0), (2,3), (4,6)} = 3.

Free forever — sign in with email at poorvithmp.com to unlock the dry run, verified Python 3 solution, edge-case traps, and downloads.

Back to Python Notes Hub