HackerRank Solution: Python Division [2 Methods]


Written by - Bashir Alam
Reviewed by - Deepak Prasad

Question: Python Division [Introduction]

Tasks

The provided code stub reads two integers, a and b, from STDIN. Add logic to print two lines. The first line should contain the result of integer division, a // b. The second line should contain the result of float division, a / b.

No rounding or formatting is necessary.

Example:

a = 3
b = 5

  • The result of the integer division 3//5 = 0.
  • The result of the float division is 3/5 = 0.6.

Print

0
0.6

Input Format:

The first line contains the first integer, a.
The second line contains the second integer, b.

Output format:

Print the two lines as described above.

Sample Input 0:

4
3

Sample Output 0:

1
1.33333333333

 

Possible Solutions

Now, let us discuss different possible solutions to the given problem. The following Python code is already given there:

if __name__ == '__main__':
    a = int(input())
    b = int(input())

Here we will discuss the following two ways to solve the problem:

  1. Using a single print method
  2. Using multiple print methods

 

Solution-1: Using the single print method

Now, we will use a single print method to solve the problem.

if __name__ == '__main__':
    a = int(input())
    b = int(input())
    
    print(str(a//b)+"\n"+str(float(a/b)))

This code defines a block of code that will only be executed if the script is run directly (as opposed to being imported as a module by another script). When the script is run, it will prompt the user to input two integers, which it will store in the variables a and b. It will then perform integer division on a and b, storing the result in a new variable c. It will then print c followed by the result of a divided by b as a float, with a newline character between them.

 

Solution-2: Using multiple print methods

Now, we will use multiple print methods to solve the problem:

if __name__ == '__main__':
    a = int(input())
    b = int(input())
    
    print(a//b)
    print(a/b)

This code defines a block of code that will only be executed if the script is run directly (as opposed to being imported as a module by another script). When the script is run, it will prompt the user to input two integers, which it will store in the variables a and b. It will then perform integer division on a and b, printing the result. It will then perform regular division on a and b, printing the result.

 

Summary

In this short article, we discussed how to solve the Python division method on HackerRank. We discussed a couple of methods and explained both solutions.

 

Further Reading

Question on Hacker Rank: Python Division [Introduction]

 

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and predictive models. You can reach out to him on his Linkedin or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment