Hacker Rank - Python Introduction
Python Version 3
Problems
- Say “Hello, World!” With Python
 - Python If-Else
 - Arithmetic Operators
 - Python: Division
 - Loops
 - Write a function
 - Print function
 
Say “Hello, World!” With Python
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Print the required string.
 Solution:
print("Hello, World!")
Python If-Else
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Read input from user.
 - Validate user input, constraints
 1 <= n <= 100.- Print Weird if number is odd.
 - Else, If number is between 6 and 20 inclusive then print Weird, else, print Not Weird.
 Solution:
#!/bin/python3 import math import os import random import re import sys def is_valid_input(num): if (num >= 1 and num <= 100): return True return False def solution(num): if(is_valid_input(num)): if (num % 2 != 0): print("Weird") else: if(num >= 6 and num <= 20): print("Weird") else: print("Not Weird") if __name__ == '__main__': n = int(input().strip()) solution(n)
Arithmetic Operators
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Read input from user.
 - Validate user input, constraints
 1 <= a,b <= 10 ^ 10.- Print sum, difference and product.
 Solution:
lower_bound = 1 upper_bound = 10 ** 10 def is_valid_input(num): if(num >= lower_bound and num <= upper_bound): return True return False def solution(num1, num2): if(is_valid_input(num1) and is_valid_input(num2)): print(num1 + num2) print(num1 - num2) print(num1 * num2) if __name__ == '__main__': a = int(input()) b = int(input()) solution(a, b)
Python: Division
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Read input from user.
 - Print the result of integer division in line 1 and result of float division in line 2.
 Solution:
def solution(num1, num2): print(num1 // num2) print(num1 / num2) if __name__ == '__main__': a = int(input()) b = int(input()) solution(a, b)
Loops
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Read input from user.
 - Validate user input, constraints
 1 <= n <= 20.- Iterate through each number in range of 0 until n and print square of number.
 Solution:
def is_valid_input(num): if(num >= 1 and num <= 20): return True return False def solution(num): if(is_valid_input(num)): for i in range(0, num): print(i ** 2) if __name__ == '__main__': n = int(input()) solution(n)
Write a function
Problem -> URL
Difficulty Level: Medium
Pseudocode:
- Read input from user.
 - Validate user input, constraints
 1900 <= year <= 10 ^ 5.- If year divisible by 4 and divisible by 100, then year is not leap year.
 - If year divisible by 4 and divisible by 400 or only divisible by 4, then year is leap.
 - All other cases, year will not be leap.
 Solution:
lower_bound = 1900 upper_bound = 10 ** 5 def is_valid_input(n): if(n >= lower_bound and n <= upper_bound): return True return False def is_leap(year): if(is_valid_input(year)): if(year % 4 == 0): if (year % 400 == 0): return True elif (year % 100 == 0): return False else: return True return False year = int(input()) print(is_leap(year))
Print Function
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Read input from user.
 - Validate user input, constraints
 1 <= n <= 150.- For number in range of 1 to n, use print and replace default end character ‘\n’ with empty string.
 Solution:
def is_valid_input(num): if(num >= 1 and num <= 150): return True return False def solution(num): if (is_valid_input(num)): for i in range(1, num + 1): print(i, end="") if __name__ == '__main__': n = int(input()) solution(n)