Classes Dealing with Complex Numbers in python - HackerRank Solution
Problem :
For this challenge, you are given two complex numbers, and you have to print the result of their addition, subtraction, multiplication, division and modulus operations.
The real and imaginary precision part should be correct up to two decimal places.
Input Format :
One line of input: The real and imaginary part of a number separated by a
space.
Output Format :
For two complex numbers C and D, the output should be in the following
sequence on separate lines:
- C + D
- C - D
- C * D
- C / D
- Mod(C)
- Mod(D)
A + Bi
Replace the plus symbol (+) with a minus symbol (-) when B < 0.
For complex numbers with a zero complex part i.e. real numbers, the output
should be:
A + 0.00i
For complex numbers where the real part is zero and the complex part is
non-zero, the output should be:
0.00 + Bi
Sample Input :
2 1 5 6
Sample Output :
7.00+7.00i -3.00-5.00i 4.00+17.00i 0.26-0.11i 2.24+0.00i 7.81+0.00i
Concept :
Methods with a double underscore before and after their name are considered as
built-in methods. They are used by interpreters and are generally used in the
implementation of overloaded operators or other built-in functionality.
__add__-> Can be overloaded for + operation
__sub__ -> Can be overloaded for - operation
__mul__ -> Can be overloaded for * operation
For more information on operator overloading in Python, refer
here.
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | # Classes Dealing with Complex Numbers in python - Hacker Rank Solution # Python 3 import math class Complex(object): # Classes Dealing with Complex Numbers in python - Hacker Rank Solution START def __init__(self, real, imaginary): self.real = real self.imaginary = imaginary def __add__(self, no): real = self.real + no.real imaginary = self.imaginary + no.imaginary return Complex(real,imaginary) def __sub__(self, no): real = self.real - no.real imaginary = self.imaginary - no.imaginary return Complex(real,imaginary) def __mul__(self, no): real = self.real * no.real - self.imaginary * no.imaginary imaginary = self.real * no.imaginary + self.imaginary * no.real return Complex(real,imaginary) def __truediv__(self, no): x = float(no.real ** 2 + no.imaginary ** 2) y = self * Complex(no.real, -no.imaginary) real = y.real / x imaginary = y.imaginary / x return Complex(real, imaginary) def mod(self): real = math.sqrt(self.real ** 2 + self.imaginary ** 2) return Complex(real, 0) # Classes Dealing with Complex Numbers in python - Hacker Rank Solution END def __str__(self): if self.imaginary == 0: result = "%.2f+0.00i" % (self.real) elif self.real == 0: if self.imaginary >= 0: result = "0.00+%.2fi" % (self.imaginary) else: result = "0.00-%.2fi" % (abs(self.imaginary)) elif self.imaginary > 0: result = "%.2f+%.2fi" % (self.real, self.imaginary) else: result = "%.2f-%.2fi" % (self.real, abs(self.imaginary)) return result if __name__ == '__main__': c = map(float, input().split()) d = map(float, input().split()) x = Complex(*c) y = Complex(*d) print(*map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()]), sep='\n') |
Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.
Can you explain this program