forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathM0014.py
More file actions
30 lines (22 loc) · 958 Bytes
/
M0014.py
File metadata and controls
30 lines (22 loc) · 958 Bytes
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
# Problem Statement: https://www.hackerrank.com/challenges/class-1-dealing-with-complex-numbers/problem
import math
class Complex(object):
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __add__(self, no):
return Complex(self.real + no.real, self.imaginary + no.imaginary)
def __sub__(self, no):
return Complex(self.real - no.real, self.imaginary - no.imaginary)
def __mul__(self, no):
return Complex(self.real*no.real - self.imaginary*no.imaginary,
self.real*no.imaginary + self.imaginary*no.real)
def __truediv__(self, no):
x = self.real*no.real + self.imaginary*no.imaginary
y = self.imaginary*no.real - self.real*no.imaginary
z = no.real*no.real + no.imaginary*no.imaginary
return Complex(x / z, y / z)
def mod(self):
return Complex(math.sqrt(self.real*self.real + self.imaginary*self.imaginary), 0)
def __str__(self):
return f'{self.real:.2f}{self.imaginary:+.2f}i'