ComplexNumber Class
You are asked to create a class ComplexNumber
that encapsulates the behavior of a complex number. A complex number is a number that can be expressed in the form a + bi
, where a
and b
are real numbers, and i
is a solution of the equation . Because no real number satisfies this equation,
i
is called an imaginary number ().
Your ComplexNumber
class should have the following attributes:
real
: The real part of the complex number (a
)imaginary
: The imaginary part of the complex number (b
)
You have to implement these magic methods:
__add__
: This method should add two complex numbers and return a newComplexNumber
object that represents the result:.
__sub__
: This method should subtract one complex number from another and return a newComplexNumber
object that represents the result:.
__mul__
: This method should multiply two complex numbers and return a newComplexNumber
object that represents the result:.
__truediv__
: This method should divide one complex number by another and return a newComplexNumber
object that represents the result:.
Besides the numerical magic methods, you should also implement a __str__
magic method that would return the complex number as <real> + <imaginary> i
.
Input | Output |
---|---|
| 3 + 3 i |
Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB