Python3 hackerRank exercise with solutions (Set 4)

1. Python If-Else 

Given an integer, n, 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

Input format :
A single line containing an integer N. (i.e.N>0)

Output format :
Print weird if the number is weird. Else print Not weird

Sample input :
3

Sample output :
Weird

Code :

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

2. Arithmetic Operators
The code reads two integers a and b from user and print three lines where,
  • the first line contains the sum of two integers, a+b
  • the second line contains the difference of two integers, a-b
  • the third line contains the product of two integers, a*b
Input format :
The first line contains the integer a.
The second line contains the integers b.

Output format :
The first line contains the sum of two integers, a+b.
The second line contains the difference of two integers, a-b.
The third line contains the product of two integers, a*b.

Sample input :
3
2

Sample output :
5
1
6

Code :

a=int(input())
b=int(input())
#sum of integers
print(a+b)
#difference of integers
print(a-b)
#product of integers
print(a*b)

3. Division
The code reads two integers a and b from user and print two lines where,
  • the first line contains the result of integer division, a//b
  • the second line contains the result of float division, a/b
Input format :
The first line contains the integer a.
The second line contains the integers b.

Output format :
The first line contains the result of integer division, a//b
The second line contains the result of float division, a/b

Sample input :
4
3

Sample output :
1
1.33333333333

Code :

a=int(input())
b=int(input())
#integer division
print(a//b)
#float division
print(a/b)

4. Loops
The code reads an integer n where, n>0. For the list of non-negative integers
that are less than n, print the square of the elements on separate lines.

Input format :
A single line containing an integer, n.

Output format :
Print n lines, one corresponding to each i.

Sample input :
5

Sample output :
0
1
4
9
16

Code :

n=int(input()) for i in range(0,n): print(i*i)

5. Find the Runner-Up Score!
Given the participant's score sheet for you university sports day, you are
required to find the runner up score. You are given n scores. Store them in a
list and find the score of the runner-up.

Input format :
The first line contains an integer n. The second line contains an array of
integers
each separated by space.

Output format :
Print the runner-up score.

Sample input :
5
2 3 6 6 5

Sample output :
5

Code :

n=int(input())
#array input arr = map(int, input().split()) arr=list(arr) new = [] for i in arr: if i not in new: new.append(i) new.sort() print(new[-2])


Comments

Popular posts from this blog

Finding the percentage in python - HackerRank solution

HackerRank challenges with solutions (Rearrangement, Formula, Word sorting, Numbers, Sentence)

ginortS in Python - HackerRank solution