Header Ad

HackerRank classes: Dealing with complex numbers solution in python

In this Classes: Dealing with complex numbers 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.

HackerRank classes: Dealing with complex numbers solution in python


Problem solution in Python 2 programming.

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());


Problem solution in Python 3 programming.

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




Problem solution in pypy programming.

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


Problem solution in pypy3 programming.

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)


Post a Comment

0 Comments