# ESC Homework 1 ### 1. Cohort Exercise 1.2 API: ![](https://i.imgur.com/mPGcHxK.png) ### 2. Cohort Exercise 1.3 API: ![](https://i.imgur.com/f5OihOU.png) ### 3. Cohort Exercise 2 API: ![](https://i.imgur.com/gKmT0OL.png) <br/> Python Code without using inbuilt class: ``` #although Python has a complex number class built-in, it is done without to justify converting to other languages class ComplexNumber: def __init__(self, real, img): self.r = real self.i = img def conjugate(self): return ComplexNumber(self.r,-self.i); def __add__(self,otherCmplx): return ComplexNumber(self.r + otherCmplx.r,self.i + otherCmplx.i) def __sub__(self,otherCmplx): return ComplexNumber(self.r - otherCmplx.r,self.i - otherCmplx.i) def __mul__(self,otherCmplx): new_r = (self.r * otherCmplx.r) + (-1*(self.i * otherCmplx.i)) new_i = (self.r * otherCmplx.i) + (self.i * otherCmplx.r) return ComplexNumber(new_r, new_i) def __truediv__(self,otherCmplx): other_cj = otherCmplx.conjugate() denom = otherCmplx * other_cj denom_r = denom.r nom = self * other_cj return ComplexNumber(nom.r/denom_r,nom.i/denom_r) __floordiv__ = __truediv__ class Report: def __init__(self): firstCmplx = [None,None] secondCmplx = [None,None] while(firstCmplx[0]==None and firstCmplx[1]==None): try: firstCmplx_inp = input("Give me a complex number(only digits) in format a,b: ").split(","); firstCmplx[0] = float(firstCmplx_inp[0]) firstCmplx[1] = float(firstCmplx_inp[1]) except IndexError: print("Wrong Format, Please try again") self.CmplxFirst = ComplexNumber(firstCmplx[0],firstCmplx[1]) while(secondCmplx[0]==None and secondCmplx[1]==None): try: secondCmplx_inp = input("Give me the other complex number(only digits) in format a,b: ").split(","); secondCmplx[0] = float(secondCmplx_inp[0]) secondCmplx[1] = float(secondCmplx_inp[1]) except IndexError: print("Wrong Format, Please try again") self.CmplxSecond = ComplexNumber(secondCmplx[0],secondCmplx[1]) self.execute() def execute(self): swap = False done = False print("Report:") while(swap==False or done==False): if(swap): temp = self.CmplxSecond self.CmplxSecond = self.CmplxFirst self.CmplxFirst = temp done = True print("First Cmplx Number: %f , %fi" % (self.CmplxFirst.r,self.CmplxFirst.i)) print("Second Cmplx Number: %f , %fi"%(self.CmplxSecond.r,self.CmplxSecond.i)) add = self.CmplxFirst + self.CmplxSecond print("Addition: %f , %fi" % (add.r,add.i)) sub = self.CmplxFirst - self.CmplxSecond print("Subtraction: %f , %fi" % (sub.r,sub.i)) mul = self.CmplxFirst * self.CmplxSecond print("Multiply: %f , %fi" % (mul.r,mul.i)) div = self.CmplxFirst / self.CmplxSecond print("Division: %f , %fi" % (div.r,div.i)) print("\n") swap = True Report() ``` ### 4. Cohort Exercise 4 ![](https://i.imgur.com/HoRGd1Q.png) ### 5. Cohort Exercise 5 ![](https://i.imgur.com/N2jkBLO.png) ### 6. Cohort Exercise 6 ![](https://i.imgur.com/NTKzDQ7.png) ### 7. Cohort Exercise 7 ![](https://i.imgur.com/hc1pkVd.png) ### 8. Cohort Exercise 8 ![](https://i.imgur.com/wWIpfQ0.png)