HackerRank Questions and Answers
HackerRank Questions and Answers by Aryadrj
Task 1
Read two integers and print two lines. The first line should contain integer division, // . The second line should contain float division, / . You don't need to perform any rounding or formatting operations.
Sample Input
4
3
Sample Output
1
1.33333333333
Program:
a=int(input())
b=int(input())
print(a//b)
print(a/b)
***
Task 2
Given an integer, , perform the following conditional actions:
If is odd, print Weird
If is even and in the inclusive range of to , print Not Weird
If is even and in the inclusive range of to , print Weird
If is even and greater than , print Not Weird.
Program:
n=int(input(""))
if(n>20):
if(n%2==0):
print("Not Weird")
else:
print("Weird")
if(n<20):
if (n%2!=0):
print("Weird")
if n>=2 and n<=5:
if(n%2==0):
print("Not Weird")
if n>=6 and n<=20:
if(n%2==0):
print("Weird")
***
Task 3
We add a Leap Day on February 29, almost every four years. The leap day is an extra, or intercalary day and we add it to the shortest month of the year, February.
In the Gregorian calendar three criteria must be taken into account to identify leap years:
The year can be evenly divided by 4, is a leap year, unless:
The year can be evenly divided by 100, it is NOT a leap year, unless:
The year is also evenly divisible by 400. Then it is a leap year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.Source
You are given the year, and you have to write a function to check if the year is leap or not.
Note that you have to complete the function and remaining code is given as template.
program:
def is_leap(year):
leap = False
if year%4==0:
if year%100==0:
if year%400==0:
leap=True
else:
leap=False
else:
leap=True
else:
leap=False
return leap
year = int(input())
print(is_leap(year))
***
Task 4
Read an integer .
Without using any string methods, try to print the following:
Note that "" represents the values in between.
Sample Input 0
3
Sample Output 0
123
program
n=int(input())
sum=0
for i in range(n):
sum=sum+1
print(sum,end="")
***
Task 5
You are given the firstname and lastname of a person on two different lines. Your task is to read them and print the following:
Hello firstname lastname! You just delved into python.
Sample Input
Ross
Taylor
Sample Output
Hello Ross Taylor! You just delved into python.
def print_full_name(a,b):
print("Hello "+a+" "+b+"! "+"You just delved into python.")
if __name__ == '__main__':
first_name = input()
last_name = input()
print_full_name(first_name, last_name)
dd
***
Task 6
You are asked to ensure that the first and last names of people begin with a capital letter in their passports. For example, alison heck should be capitalised correctly as Alison Heck.
Given a full name, your task is to capitalize the name appropriately.
program
import math
import os
import random
import re
import sys
import string
# Complete the solve function below.
def solve(s):
c=string.capwords(s, sep=" ")
return c
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
result = solve(s)
fptr.write(result + '\n')
fptr.close()
***
Task 7
In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.
Sample Input
ABCDCDC
CDC
Sample Output
2
def count_substring(a,occ):
s = a
sb = occ
results = 0
sub_len = len(sb)
for i in range(len(s)):
if s[i:i+sub_len] == sb:
results += 1
return results
if __name__ == '__main__':
string = input().strip()
sub_string = input().strip()
count = count_substring(string, sub_string)
print(count)
***
Task 8
You are given a string and width .
Your task is to wrap the string into a paragraph of width .
Input Format
The first line contains a string, .
The second line contains the width, .
Sample Input 0
ABCDEFGHIJKLIMNOQRSTUVWXYZ
4
Sample Output 0
ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ
Program:
import textwrap
def wrap(string, max_width):
wrapper=textwrap.TextWrapper(width=max_width)
word_list=wrapper.wrap(text=string) #this line converts string into list
for element in word_list:
print(element)
return ""
if __name__ == '__main__':
string, max_width = input(), int(input())
result = wrap(string, max_width)
print(result)
***
Task 9
Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.
Sample Input 0
5
2 3 6 6 5
Sample Output 0
5
Program
if __name__ == '__main__':
input1 = int(input())
arr =list(map(int, input().split(" ")))
k = max(arr)
for i in range(0,input1):
if max(arr) == k:
arr.remove(max(arr))
arr.sort(reverse=True)
print(arr[0])
Comments
Post a Comment