diff --git a/__pycache__/__init__.cpython-36.pyc b/__pycache__/__init__.cpython-36.pyc index a600305..419c1ba 100644 Binary files a/__pycache__/__init__.cpython-36.pyc and b/__pycache__/__init__.cpython-36.pyc differ diff --git a/q01_create_class/__pycache__/__init__.cpython-36.pyc b/q01_create_class/__pycache__/__init__.cpython-36.pyc index 09a1efa..31dc6c1 100644 Binary files a/q01_create_class/__pycache__/__init__.cpython-36.pyc and b/q01_create_class/__pycache__/__init__.cpython-36.pyc differ diff --git a/q01_create_class/__pycache__/build.cpython-36.pyc b/q01_create_class/__pycache__/build.cpython-36.pyc index 9f53117..135aee3 100644 Binary files a/q01_create_class/__pycache__/build.cpython-36.pyc and b/q01_create_class/__pycache__/build.cpython-36.pyc differ diff --git a/q01_create_class/build.py b/q01_create_class/build.py index a0188d6..866e540 100644 --- a/q01_create_class/build.py +++ b/q01_create_class/build.py @@ -1,15 +1,71 @@ +# %load q01_create_class/build.py import pandas as pd import numpy as np import math -"write your solution here" +'write your solution here' -class complex_number: - """The complex number class. +class complex_number(object): + '''The complex number class. Attributes: attr1 (x): Real part of complex number. attr2 (y): Imaginary part of complex number. - """ + ''' + + def __init__(self,real=0.0,imag=0.0): + self.real = real + self.imag = imag + + def __str__(self): + if self.imag < 0.0: + return '{}-{}i'.format(self.real,abs(self.imag)) + else: + return '{}+{}i'.format(self.real,self.imag) + + + def __add__(self,other): + c = self.real + other.real + d = self.imag + other.imag + return complex_number(c,d) + + def __sub__(self, others): + return complex_number(self.real - others.real, self.imag - others.imag) + + def __mul__(self, others): + return complex_number(self.real*others.real-self.imag*others.imag, self.real*others.imag+self.imag*others.real) + + def __truediv__(self,others): + a = (self.real*others.real+self.imag*others.imag)/(others.real**2 + others.imag**2) + b = (self.imag*others.real-self.real*others.imag)/(others.real**2 + others.imag**2) + return a,b + + + def abs(self): + return np.sqrt(np.square(self.real)+np.square(self.imag)) + + def argument(self): + return math.degrees(math.atan(self.real/self.imag)) + + + def conjugate(self): + return complex_number(self.real, -self.imag) + +d1 = complex_number(5,5) +print(d1) + +d1 = complex_number(5,1) +d2 = complex_number(6,0) +print(d1 + d2) +# print(d2 - d1) +print(d2 / d1) +# print(d2 * d1) +# print(d1.conjugate()) +# print(d2.conjugate()) +# print(d1.abs()) +# print(d2.abs()) +# print(d1.argument()) + + diff --git a/q01_create_class/tests/__pycache__/__init__.cpython-36.pyc b/q01_create_class/tests/__pycache__/__init__.cpython-36.pyc index 58575f1..8895003 100644 Binary files a/q01_create_class/tests/__pycache__/__init__.cpython-36.pyc and b/q01_create_class/tests/__pycache__/__init__.cpython-36.pyc differ diff --git a/q01_create_class/tests/__pycache__/test_complex_number.cpython-36.pyc b/q01_create_class/tests/__pycache__/test_complex_number.cpython-36.pyc index b378e09..bf8b85d 100644 Binary files a/q01_create_class/tests/__pycache__/test_complex_number.cpython-36.pyc and b/q01_create_class/tests/__pycache__/test_complex_number.cpython-36.pyc differ