HackerRank Solution: Python If-Else [3 Methods]


Hacker Rank Python

Author: Bashir Alam
Reviewer: Deepak Prasad

Question: Python if-else [Introduction]

Task:

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:

1 <= n <=10

Output format:

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

Sample input 0:

3

Sample output 0:

Weird

Explanation:

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

 

Possible solutions

Now let us move toward solutions. We have already been given the following code.

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    n = int(input().strip())

Here we will discuss the following possible solutions.

  1. Using if-else statements
  2. Using multiple conditions in one if statements
  3. Using nested if-else statements

 

Solution-1: Using if-else statements

Let us use if-else statements to solve the problem:

if __name__ == '__main__':
    n = int(input().strip())
    if n % 2 != 0:
        print("Weird")
    elif n in range(2,6):
        print("Not Weird")
    elif n in range(6,21):
        print("Weird")
    elif n > 20:
        print("Not Weird")

This code determines whether a given integer is "Weird" or "Not Weird" based on the following criteria:

  • If the integer is odd, it is "Weird".
  • If the integer is even and in the range 2 to 5 (inclusive), it is "Not Weird".
  • If the integer is even and in the range 6 to 20 (inclusive), it is "Weird".
  • If the integer is even and greater than 20, it is "Not Weird".

The code first checks if the integer is odd. If it is, it prints "Weird" and the program ends. If the integer is even, the code checks if it is in the appropriate range according to the above criteria, and prints the appropriate string. If the integer is neither odd nor in any of the specified ranges, the program will not print anything.

 

Solution-2: Using multiple conditions in one if statements

Now we will use multiple conditions in one if statement to solve the problem:

if __name__ == '__main__':
    n = int(input().strip()) 
    oddcheck = (n % 2) != 0
    if oddcheck or (n > 100) or (n < 1) or (n >= 6 and n <= 20):
        print("Weird")
    else:
        print("Not Weird")

This code determines whether a given integer is "Weird" or "Not Weird" based on the following criteria:

  • If the integer is odd or greater than 100 or less than 1 or in the range 6 to 20 (inclusive), it is "Weird".
  • If the integer is even and in the range 2 to 5 (inclusive) or greater than 20, it is "Not Weird".

The code first checks if the integer is odd or out of the specified range. If it is, it prints "Weird" and the program ends. If the integer is even and in the appropriate range according to the above criteria, the code prints "Not Weird". If the integer is neither odd nor in any of the specified ranges, the program will not print anything.

 

Solution-3: Using nested if-else statements

Now we will use the nested if-else statements to solve the problem.

if __name__ == '__main__':
    n = int(input().strip())
    if n % 2 != 0:
        print('Weird')
    else:
        if n >= 2 and n <= 5:
            print('Not Weird')
        elif n >= 6 and n <= 20:
            print('Weird')
        else:
            print('Not Weird')

This code determines whether a given integer is "Weird" or "Not Weird" based on the following criteria:

  • If the integer is odd, it is "Weird".
  • If the integer is even and in the range 2 to 5 (inclusive), it is "Not Weird".
  • If the integer is even and in the range 6 to 20 (inclusive), it is "Weird".
  • If the integer is even and greater than 20, it is "Not Weird".

The code first checks if the integer is odd. If it is, it prints "Weird" and the program ends. If the integer is even, the code checks if it is in the appropriate range according to the above criteria using an if-elif-else block, and prints the appropriate string. If the integer is neither odd nor in any of the specified ranges, the program will not print anything.

 

Summary

In this short article, we discussed how we can solve Python if-else problem on hacker rank. We discussed three different methods to solve the problem.

 

Further readings

Question on hacker rank: Python if else [Introduction]

 

Bashir Alam

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 Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

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