Classes Dealing with Complex Numbers Hacker Rank Solution – Queslers

Problem : Classes Dealing with Complex Numbers Hacker Rank Solution

For this challenge, you are given two complex numbers, and you have to print the result of their addition, subtraction, multiplication, division and modulus operations.
The real and imaginary precision part should be correct up to two decimal places.


Input Format :

One line of input: The real and imaginary part of a number separated by a space.

Output Format :

For two complex numbers C and D, the output should be in the following sequence on separate lines:

  • C + D
  • C – D
  • C * D
  • C / D
  • Mod(C)
  • Mod(D)

For complex numbers with non-zero real (A) and complex part (B), the output should be in the following format:
A + Bi
Replace the plus symbol (+) with a minus symbol (-) when B < 0.
For complex numbers with a zero complex part i.e. real numbers, the output should be:A + 0.00i
For complex numbers where the real part is zero and the complex part is non-zero, the output should be:0.00 + Bi


Sample Input :

2 1
5 6

Sample Output :

7.00+7.00i
-3.00-5.00i
4.00+17.00i
0.26-0.11i
2.24+0.00i
7.81+0.00i

Concept :

Python is a fully object-oriented language like C++, Java, etc. For reading about classes, refer here.
Methods with a double underscore before and after their name are considered as built-in methods. They are used by interpreters and are generally used in the implementation of overloaded operators or other built-in functionality.

__add__-> Can be overloaded for + operation
__sub__ -> Can be overloaded for - operation
__mul__ -> Can be overloaded for * operation


Classes Dealing with Complex Numbers Hacker Rank Solution in python 2

import math
class Complex:
    def __init__(self, a, b):
        self.a, self.b = a, b;
    def display(self):
        if self.a < 0.005 and self.a > -0.005:
            self.a = 0;
        if self.b < 0.005 and self.b > -0.005:
            self.b = 0;
        if self.a != 0:
            str1 = '%0.2f' % self.a;
            if self.b > 0:
                str1 += ' + %0.2fi' % self.b;
            elif self.b < 0:
                str1 += ' - %0.2fi' % -self.b;
        elif self.b != 0:
            str1 = '%0.2fi' % self.b;
        else:
            str1 = '0.00';
        print str1;
        
    def conjugate(self):
        return Complex(self.a, -self.b);
    def norm(self):
        return self.a*self.a + self.b*self.b;
    def scale(self, scalar):
        return Complex(self.a*scalar, self.b*scalar);

    
def add(a, b):
    return Complex(a.a + b.a, a.b + b.b);
def sub(a, b):
    return Complex(a.a - b.a, a.b - b.b);
def mul(a, b):
    return Complex(a.a * b.a - a.b * b.b, a.a * b.b + a.b * b.a);
def div(a, b):
    return mul(a, b.conjugate().scale(1.0/b.norm()));
    
x = Complex(0, 0);
y = Complex(0, 0);

[a, b] = raw_input().split();
x.a = float(a);
x.b = float(b);

[a, b] = raw_input().split();
y.a = float(a);
y.b = float(b);

add(x, y).display();
sub(x, y).display();
mul(x, y).display();
div(x, y).display();
print '%0.2f' % math.sqrt(x.norm());
print '%0.2f' % math.sqrt(y.norm());

Classes Dealing with Complex Numbers Hacker Rank Solution in python 3

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):
        r=self.real*no.real-self.imaginary*no.imaginary
        i=self.real*no.imaginary+self.imaginary*no.real
        return Complex(r,i)

    def __truediv__(self, no):
        d=no.real**2+no.imaginary**2
        n=self*Complex(no.real,-1*no.imaginary)
        return Complex(n.real/d,n.imaginary/d)


    def mod(self):
        d=self.real**2+self.imaginary**2
        return Complex(math.sqrt(d),0)
    def __str__(self):
        if self.imaginary == 0:
            result = "%.2f+0.00i" % (self.real)
        elif self.real == 0:
            if self.imaginary >= 0:
                result = "0.00+%.2fi" % (self.imaginary)
            else:
                result = "0.00-%.2fi" % (abs(self.imaginary))
        elif self.imaginary > 0:
            result = "%.2f+%.2fi" % (self.real, self.imaginary)
        else:
            result = "%.2f-%.2fi" % (self.real, abs(self.imaginary))
        return result

Classes Dealing with Complex Numbers Hacker Rank Solution in pypy

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 __div__(self, no):
        try: 
            return self.__mul__(Complex(no.real, -1*no.imaginary)).__mul__(Complex(1.0/(no.mod().real)**2, 0))
        except ZeroDivisionError as e:
            print e
            return None
        
    def mod(self):
        return Complex(pow(self.real**2+self.imaginary**2, 0.5), 0)

    def __str__(self):
        if self.imaginary == 0:
            result = "%.2f+0.00i" % (self.real)
        elif self.real == 0:
            if self.imaginary >= 0:
                result = "0.00+%.2fi" % (self.imaginary)
            else:
                result = "0.00-%.2fi" % (abs(self.imaginary))
        elif self.imaginary > 0:
            result = "%.2f+%.2fi" % (self.real, self.imaginary)
        else:
            result = "%.2f-%.2fi" % (self.real, abs(self.imaginary))
        return result

Classes Dealing with Complex Numbers Hacker Rank Solution in pypy 3

from math import hypot

class Complex(object):
    
    def __init__(self, real, imaginary):
        self.re = real
        self.im = imaginary
        
    def __add__(self, no):
        return Complex(self.re + no.re, self.im + no.im)

    def __sub__(self, no):
        return Complex(self.re - no.re, self.im - no.im)
        
    def __mul__(self, no):
        return Complex(
            self.re * no.re - self.im * no.im,
            self.im * no.re + self.re * no.im)

    def __truediv__(self, no):
        return Complex(
            (self.re * no.re + self.im * no.im) / (no.re**2 + no.im**2),
            (self.im * no.re - self.re * no.im) / (no.re**2 + no.im**2))
        
    def mod(self):
        return Complex(hypot(self.re, self.im), 0.0)

    def __str__(self):
        return "{:.2f}{:+.2f}i".format(self.re, self.im)
Classes Dealing with Complex Numbers Hacker Rank Solution Review:

In our experience, we suggest you solve this Classes Dealing with Complex Numbers Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Classes Dealing with Complex Numbers is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution

Classes Dealing with Complex Numbers Hacker Rank Solution Conclusion:

I hope this Classes Dealing with Complex Numbers Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Mini-Max Sum Hacker Rank Solution

String Validators Hacker Rank Solution

Text Alignment Hacker Rank solution

String validators Hacker Rank Solution

Staircase Hacker Rank Solution

Leave a Reply

Your email address will not be published.