Complex Numbers Sum and Product

Complex Numbers Sum and Product

Medium Python OOP Basics 13 views
Explanation Complexity

Problem Statement

Read two complex numbers as a b and c d, meaning (a+bi) and (c+di). 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.

Example

1 2 3 4
4 6
-5 10

Constraints

|values|

Input / Output Format

Input Format
One line: a b c d.
Output Format
Two lines: sumReal sumImag then prodReal prodImag.
Constraints
|values|

Examples

Input:
1 2 3 4
Output:
4 6 -5 10

Example Solution (Public)

Python
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}')

Official Solution Code

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}')
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.