Complex Numbers Sum and Product
Programming Interview
Medium
8 views
Problem Description
Consider {x}. Create Complex class with add and mul. Output sum then product as 'x y' for real and imag.
Input Format
One line: a b c d.
Output Format
Two lines: sumReal sumImag then prodReal prodImag.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<4: sys.exit(0)
a=int(p[0]); b=int(p[1]); c=int(p[2]); d=int(p[3])
class Complex:
def __init__(self,re,im):
self.re=re
self.im=im
def add(self,other):
return Complex(self.re+other.re, self.im+other.im)
def mul(self,other):
return Complex(self.re*other.re - self.im*other.im, self.re*other.im + self.im*other.re)
x=Complex(a,b)
y=Complex(c,d)
s=x.add(y)
m=x.mul(y)
print(f'{s.re} {s.im}')
print(f'{m.re} {m.im}')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!