Scientyfic World

Write Structured content faster — Get your copy of DITA Decoded today. Buy it Now

HackerRank solution in Python (easy)

HackerRank is the place where we can use our learnings to solve various problems given there and make ourselves pro in coding. But in case you are in trouble solving those questions then here are the solutions.
All these solutions are compiled and successfully passed all the tests. So no worries.
Just one thing and that is before copying these codes understand the logic.
Best of luck

Python If-Else


#!/bin/python3

import math
import os
import random
import re
import sys



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

Arithmetic Operators


if __name__ == '__main__':
a = int(input())
b = int(input())
print(a+b)
print(a-b)
print(a*b)

Python:Division


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

Loops:


if __name__ == '__main__':
n = int(input())
for i in range (0,n):
print (i*i)

Write a function:


def is_leap(year):
leap = False

# Write your logic here
if year%4==0:
leap = True
if year%400==0:
leap = True
if year%100==0 and (year %400 != 0):
leap = False


return leap

year = int(input())
print(is_leap(year))

Print Function:


if __name__ == '__main__':
n = int(input())
for i in range (1,n+1):
print (i, end="")

List Comprehensions:


if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())
print ([[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i+j+k != n ])

Lists:


if __name__ == '__main__':
N = int(input())
empty = []
new = []

for i in range(N):
x = input().split()
empty.append(x)

for i in range(len(empty)):
if empty[i][0] == 'insert':
x = int(empty[i][1])
y = int(empty[i][2])
new.insert(x,y)
elif empty[i][0] == 'print':
print(new)
elif empty[i][0] == 'remove':
new.remove(int(empty[i][1]))
elif empty[i][0] == 'append':
new.append(int(empty[i][1]))
elif empty[i][0] == 'sort':
new.sort()
elif empty[i][0] == 'pop':
new.pop()
elif empty[i][0] == 'reverse':
new.reverse()

Swap Case:

# Hackerrank solution for swaping letters
def swap_case(s):
    result = ''
    for letter in s:
        if letter.islower():
            result +=letter.upper()
        else:
            result += letter.lower()
    return result

if __name__ == '__main__':
    s = input()
    result = swap_case(s)
    print(result)

What’s Your Name ?

#In the main function we are taking the input from the user. Input is nothing but the first name and the last name.
 Now we're coming to the print_full_name function. From the name itself it is clear that here we're trying to print whatever the question ask us to print. So simply we'll print the first_name and the last_name.
#format() is a function that is used here to replace the first_name and last_name with the {}
#So basically after taking the inputs from the user the names are called to the upper function and there we're just putting values to the required field with help of format() function.
def print_full_name(first, last):
    # Write your code here
    print("Hello {} {}! You just delved into python.".format(first,last))

if __name__ == '__main__':
    first_name = input()
    last_name = input()
    print_full_name(first_name, last_name)

The rest of the codes will appear soon.

If you want the explanations of the answers then please let me know that in the comment section, I’ll explain the codes.

Snehasish Konger
Snehasish Konger

Snehasish Konger is a passionate technical writer and the founder of Scientyfic World, a platform dedicated to sharing knowledge on science, technology, and web development. With expertise in React.js, Firebase, and SEO, he creates user-friendly content to help students and tech enthusiasts. Snehasish is also the author of books like DITA Decoded and Mastering the Art of Technical Writing, reflecting his commitment to making complex topics accessible to all.

Articles: 234
Generate Summary
avatar of joe
Ask Joe