HackerRank: Python Solutions

GuidelineS

Use the hints to help solve the problem. If you need to check the solution, please make sure you understand the different parts of the code.


Python Division

HackerRank: PREPARE > PYTHON

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

Read up on division operators

a = int(input())
b = int(input())
print(a // b)
print(a / b)

Arithmetic Operators

HackerRank: PREPARE > PYTHON

The provided code stub reads two integers from STDIN, a and b. Add code to print three lines where:

  1. The first line contains the sum of the two numbers.
  2. The second line contains the difference of the two numbers (first – second).
  3. The third line contains the product of the two numbers.

Example
a = 3
b = 5

Print the following:

8
-2
15

Input Format

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

Constraints

Output Format

Print the three lines as explained above.

Sample Input 0

3
2

Sample Output 0

5
1
6

Explanation 0

Read up on Arithmetic Operators and taking integer input

If necessary find out what “product of the two numbers” mean

a = int(input())
b = int(input())
print(a + b)
print(a - b)
print(a * b)

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Python If-Else

HackerRank: PREPARE > PYTHON

Given an integer, n, perform the following conditional actions:

  • If n is odd, print Weird
  • If n is even and in the inclusive range of 2 to 5, print Not Weird
  • If n is even and in the inclusive range of 6 to 20, print Weird
  • If n is even and greater than 20, print Not Weird

Input Format

A single line containing a positive integer, n.

Constraints

Output Format

Print Weird if the number is weird. Otherwise, print Not Weird.

Sample Input 0

3

Sample Output 0

Weird

Explanation 0

n = 3
n is odd and odd numbers are weird, so print Weird.

Sample Input 1

24

Sample Output 1

Not Weird

Explanation 1

n = 24
n > 20 and n is even, so it is not weird.

n = int(input().strip())

if ((n % 2 == 0) and (n >= 2 and n <= 5)):
    print("Not Weird") 
elif ((n % 2 == 0) and (n >= 6 and n <= 20)):
    print("Weird")
elif ((n % 2 == 0) and (n >= 20)):
    print("Not Weird")
else:
    print("Weird")

Say “Hello, World!” With Python

HackerRank: PREPARE > PYTHON

Here is a sample line of code that can be executed in Python:

print("Hello, World!")

You can just as easily store a string as a variable and then print it to stdout:

my_string = "Hello, World!"
print(my_string)

The above code will print Hello, World! on your screen. Try it yourself in the editor below!

Input Format

You do not need to read any input in this challenge.

Output Format

Print Hello, World! to stdout.

Sample Output 0

Hello, World!
print("Hello, World!")
a = "Hello, World!"
print(a)