Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Python. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Classes Dealing with Complex Numbers in Python-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Python
Python is a widely-used, interpreted, object-oriented, and high-level programming language with dynamic semantics, used for general-purpose programming. It was created by Guido van Rossum, and first released on February 20, 1991.
Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. It is also used to create various machine learning algorithm, and helps in Artificial Intelligence. Python is a general purpose language, meaning it can be used to create a variety of different programs and isn’t specialized for any specific problems. This versatility, along with its beginner-friendliness, has made it one of the most-used programming languages today. A survey conducted by industry analyst firm RedMonk found that it was the most popular programming language among developers in 2020.
Link for the Problem – Classes Dealing with Complex Numbers in python – HackerRank Solution
Classes Dealing with Complex Numbers in python – HackerRank Solution
Problem:
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 and , the output should be in the following sequence on separate lines:
For complex numbers with non-zero real and complex part, the output should be in the following format:
Replace the plus symbol with a minus symbol when .
For complex numbers with a zero complex part i.e. real numbers, the output should be:
For complex numbers where the real part is zero and the complex part is non-zero, the output should be:
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
For more information on operator overloading in Python, refer here.
Classes Dealing with Complex Numbers in python – HackerRank Solution
import math class Complex(object): def __init__(self, real, imaginary): self.real = real self.imaginary = imaginary def __add__(self, no): a = self.real b = self.imaginary c = no.real d = no.imaginary return Complex(a + c, b + d) def __sub__(self, no): a = self.real b = self.imaginary c = no.real d = no.imaginary return Complex(a - c, b - d) def __mul__(self, no): a = self.real b = self.imaginary c = no.real d = no.imaginary real_mult = (a * c) - (b * d) imag_mult = (a * d) + (b * c) return Complex(real_mult, imag_mult) def __truediv__(self, no): a = self.real b = self.imaginary c = no.real d = no.imaginary real_numerator = a * c + b * d imag_numerator = b * c - a * d denom = c * c + d * d real_div = real_numerator / denom imag_div = imag_numerator / denom return Complex(real_div, imag_div) def mod(self): a = self.real b = self.imaginary return Complex(math.sqrt(a ** 2 + b ** 2), 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 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')
I’m not positive where you’re getting your info, however good topic. I needs to spend some time studying much more or figuring out more. Thank you for fantastic info I was searching for this information for my mission.
Some truly interesting information, well written and broadly user genial.
I love it when people come together and share opinions, great blog, keep it up.
I am glad to be a visitant of this consummate weblog! , thankyou for this rare info ! .
I don’t unremarkably comment but I gotta admit regards for the post on this one : D.
Wonderful work! That is the kind of info that are meant to be shared across the internet. Disgrace on the search engines for no longer positioning this put up upper! Come on over and seek advice from my website . Thank you =)
I like the efforts you have put in this, thanks for all the great articles.
Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts. Any way I’ll be subscribing to your augment and even I achievement you access consistently rapidly.
Hi, Neat post. There’s a problem with your web site in internet explorer, would test this… IE still is the market leader and a big portion of people will miss your wonderful writing due to this problem.
I was suggested this blog via my cousin. I am now not certain whether this put up is written through him as nobody else realize such precise about my trouble. You are amazing! Thank you!
Very interesting subject, appreciate it for posting. “He who seizes the right moment is the right man.” by Johann Wolfgang von Goethe.
Thank you for sharing excellent informations. Your web site is so cool. I am impressed by the details that you have on this site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found just the info I already searched everywhere and just couldn’t come across. What a great web-site.
Very interesting subject , regards for putting up.
Unquestionably believe that which you said. Your favorite justification appeared to be on the internet the simplest thing to be aware of. I say to you, I definitely get irked while people consider worries that they plainly do not know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people could take a signal. Will likely be back to get more. Thanks
tadalafil 40mg pill tadalafil brand best ed pills
I think this is one of the most significant information for me. And i am glad reading your article. But wanna remark on some general things, The website style is great, the articles is really great : D. Good job, cheers
duricef over the counter combivir oral proscar order
purchase fluconazole online order ampicillin 500mg without prescription order cipro 1000mg generic
order estrace online cheap minipress for sale online prazosin 1mg brand
generic metronidazole buy keflex 500mg online buy keflex 500mg for sale
cleocin 150mg sale clindamycin pill sildenafil 50mg for sale
cost avana 200mg order tadacip 10mg online cheap purchase cambia online
nolvadex without prescription nolvadex 20mg usa ceftin 250mg price
indomethacin 75mg tablet cefixime 200mg tablet cefixime pills
trimox usa arimidex medication purchase biaxin generic
buy careprost sale order robaxin pills buy desyrel 100mg sale
clonidine price buy generic tiotropium buy spiriva 9mcg generic
buy suhagra 50mg online aurogra canada sildalis where to buy
order minocycline capsules hytrin 1mg without prescription brand pioglitazone
order isotretinoin 20mg without prescription buy absorica zithromax 250mg canada
leflunomide pills oral viagra order azulfidine 500 mg generic
azithromycin generic order azipro 250mg without prescription buy neurontin 600mg without prescription
buy cialis online cheap levitra vs cialis order cialis 40mg without prescription
stromectol price us buy stromectol 3mg generic deltasone 40mg without prescription
order furosemide online cheap albuterol 4mg uk order albuterol online
order levitra 20mg online cheap plaquenil 200mg brand order hydroxychloroquine 400mg pills
I am impressed with this web site, very I am a big fan .
brand ramipril 5mg etoricoxib where to buy etoricoxib canada
buy levitra 20mg generic buy generic tizanidine over the counter cost hydroxychloroquine
order benicar without prescription depakote buy online purchase depakote pill
clobetasol canada buspirone 5mg drug buy amiodarone 100mg generic
diamox 250 mg oral acetazolamide pills azathioprine generic
order carvedilol 25mg sale aralen generic buy chloroquine generic
Great write-up, I’m normal visitor of one’s web site, maintain up the nice operate, and It is going to be a regular visitor for a long time.
purchase lanoxin generic buy micardis 80mg pills generic molnupiravir 200mg
buy naproxen 500mg pill prevacid price buy prevacid 15mg without prescription
buy proventil 100 mcg sale proventil pills how to buy phenazopyridine
buy generic olumiant online order olumiant sale brand atorvastatin 80mg
buy singulair 10mg online cheap buy singulair 10mg without prescription buy avlosulfon 100mg generic
buy nifedipine without a prescription aceon 8mg drug order generic fexofenadine 120mg
norvasc 10mg pill norvasc 10mg for sale buy prilosec online