Classes: Dealing with Complex Numbers HackerRank Solution

This Python program is about dealing with complex numbers using classes. It defines a class named 'Complex' that represents a complex number with real and imaginary parts. The class includes methods for performing basic arithmetic operations on complex numbers, such as addition, subtraction, multiplication, and division. It also includes a method for calculating the modulus of a complex number.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Classes: Dealing with Complex Numbers HackerRank Solution HackerRank solution using CodeRankGPT:

The solution works by defining a class 'Complex' that encapsulates a complex number and its operations. The class has methods for addition (__add__), subtraction (__sub__), multiplication (__mul__), and division (__truediv__) that take another 'Complex' object as an argument and return a new 'Complex' object as the result. The modulus operation is implemented in the 'mod' method, which returns the magnitude of the complex number. The '__str__' method is overridden to provide a custom string representation of the 'Complex' object. The main part of the program reads two sets of real and imaginary parts from the user, creates 'Complex' objects from them, performs the operations, and prints the results.


import math

class Complex:
    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 __truediv__(self, no):
        divider = no.real**2 + no.imaginary**2
        return Complex(
            (self.real * no.real + self.imaginary * no.imaginary) / divider,
            (self.imaginary * no.real - self.real * no.imaginary) / divider,
        )

    def mod(self):
        return Complex(math.sqrt(self.real**2 + self.imaginary**2), 0.00)

    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

if __name__ == "__main__":
    c = map(float, input().split())
    d = map(float, input().split())
    x = Complex(*c)
    y = Complex(*d)
    print(*map(str, [x + y, x - y, x * y, x / y, x.mod(), y.mod()]), sep="\n")

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform