medium +20 pts

Complex Number Class

Build a Complex class with arithmetic, equality, and string representation.

Create a class `Complex` that represents a complex number `a + bi`. The class must implement the following methods: - `__init__(self, real: float, imag: float = 0.0)` — initialize the complex number. - `__repr__(self) -> str` — return a string `'Complex(real, imag)'` where real and imag are the numeric values. Use the `str()` representation of the numbers (e.g., `Complex(1, 2)` for 1+2i). - `__str__(self) -> str` — return the algebraic form: `'a+bi'` if imag is positive, `'a-bi'` if imag is negative, `'a'` if imag is zero. Skip the imaginary part if zero. Use `str()` of the float values. For example: `Complex(1, 2)` -> `'1+2i'`, `Complex(1, -2)` -> `'1-2i'`, `Complex(1, 0)` -> `'1'`. - `__add__(self, other: 'Complex') -> 'Complex'` — return a new Complex with (a+c) + (b+d)i. - `__sub__(self, other: 'Complex') -> 'Complex'` — return a new Complex with (a-c) + (b-d)i. - `__mul__(self, other: 'Complex') -> 'Complex'` — return a new Complex using the formula `(ac - bd) + (ad + bc)i`. - `__truediv__(self, other: 'Complex') -> 'Complex'` — return a new Complex using the formula `((ac+bd)/(c^2+d^2)) + ((bc-ad)/(c^2+d^2))i`. You may assume the denominator is not zero. - `__eq__(self, other: 'Complex') -> bool` — return True if real and imag are exactly equal (use `==` on floats), False otherwise. - `conjugate(self) -> 'Complex'` — return a new Complex with the same real part and negated imaginary part. To facilitate testing, you must also implement the following standalone helper functions (outside the class) that use the `Complex` class. Note that the `str_rep` function is used for string tests and must be present. - `add(a, b) -> str` — takes two tuples `(real, imag)` and returns `repr(c1 + c2)` where c1 and c2 are Complex objects created from the tuples. - `sub(a, b) -> str` — similarly for subtraction. - `mul(a, b) -> str` — similarly for multiplication. - `div(a, b) -> str` — similarly for division. - `eq(a, b) -> bool` — takes two tuples and returns whether the corresponding Complex numbers are equal. - `conjugate(a) -> str` — takes one tuple and returns `repr(c1.conjugate())`. - `str_rep(a) -> str` — takes one tuple and returns `str(c1)`. Tests will only use real and imag values that are integers or floats with exact binary representation (e.g., 0.5, 2.0). No need for rounding.

Constraints

Real and imaginary parts are numbers (int or float). The divisor in `__truediv__` is never zero. Complexity: O(1) for all operations.

Example

>>> c1 = Complex(1, 2)
>>> c2 = Complex(3, 4)
>>> c1 + c2
Complex(4, 6)
>>> c1 - c2
Complex(-2, -2)
>>> c1 * c2
Complex(-5, 10)
>>> c1 / c2
Complex(0.44, 0.08)
>>> c1 == Complex(1, 2)
True
>>> c1.conjugate()
Complex(1, -2)
>>> str(c1)
'1+2i'
>>> str(Complex(1, -2))
'1-2i'
>>> str(Complex(1, 0))
'1'
20 points ~25 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

In __repr__, use f"Complex({self.real}, {self.imag})" to get the exact required format.
For __str__, handle three cases: imag == 0, imag > 0, imag < 0.
The division formula is (ac+bd)/(c^2+d^2) + (bc-ad)/(c^2+d^2)i.
Remember the conjugate just negates the imaginary part.
The standalone helper functions must create Complex objects from the input tuples before calling methods.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.