Python3 hackerRank exercise with solutions (Set 1)
1.DIAGONAL DIFFERENCE
Given a square matrix calculate the absolute difference between the sum of it's diagonals.
For example,
1 2 3
4 5 6
9 8 9
The left to right diagonal, 1+5+9=15
The right to left diagonal, 3+5+9=17
The absolute difference=|15-17|=2
Input format:
The first line of input contain the order of the square matrix(n).
The second line contain the matrix(A) of order
(n).
Output format:
Absolute difference of matrix A
Sample input:
3
5 8 9
2 5 7
1 4 6
Sample output:
1
Code:
Diagonal difference - Code |
Input:
Output:
2.APPLE AND ORANGE
Sam's house has an apple tree and an orange tree.In the diagram below, the red line indicates Sam's house which is between the points 's' and 't'.The orange tree is located at point 'a' towards the left of the house and orange tree is located at the point 'b' towards the right of the house.
When a fruit falls from the tree it lands 'd' units of distance from the tree on x-axis. Negative value of d indicates that the fruit falls d units towards the tree's left and positive value indicates that the fruit falls d units towards the right of the tree.
Given the value of d for m apples and n oranges determine how many apples and oranges fall on Sam's house.
For example,
Sam's house is between s=7 and t=10.
The apple tree is located at the point a=4 and the orange tree is at point b=12.
There are m=3 apples and n=2 oranges.
Apples are thrown [2,3,-4] units distance from a and oranges are thrown [3,-2,-4] units distance from b.
Adding each apple distance to the apple tree [4+2,4+3,4+(-4)]=[6,7,0] Similarly oranges land at [12+3,12+(-2),12+(-4)]=[15,10,8].
One apple and two oranges are inclusive in the range 7 and 10.
So the output should be,
1
2
Input format:
The first line contains s and t separated by space.
The second line contains a and b separated by the space.
The third line contains m and n separated by space.
The fourth line contains m space separated integers denoting the respective distance that each apple falls from point a.
The fifth line contains n space separated integers denoting the respective distance that each orange falls from the point b.
Output format:
The first line denoting the number of apples.
The second line denoting the number of oranges.
Sample input:
7 11
5 15
3 2
-2 2 1
5 -6
Sample output:
1
1
Code:
Input:
Output:
3.COMPARE THE TRIPLETS
Alice and bob created a problem individually for a competition.A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality and difficulty
We definitely the rating for Alice challenge to be a triplet a=[a[0],a[1],a[2]]
and rating for Bob challenge to be a triplet b=[b[0],b[1],b[2]].
Our task is to find their comparison points by comparing a[0] with b[0] ,a[1] with b[1] and a[2] with b[2]
If a[i]>b[i] then Alice is awarded one point.
If a[i]<b[i] then Bob is awarded one point.
If a[i]=b[i] the neither persons receives point.
Input format:
The first line contains three space separated integers a[0],a[1] and a[2] describing the respective values in triplet a.
The second line contains three space separated integers b[0],b[1] and b[2] describing the respective values in triplet b.
Output format:
Two integers denoting the respective comparison points earned by Alice and Bob.
Sample input:
17 28 30
99 16 8
Sample output:
2 1
Code:
Input:
Output:
Related articles :
Comments
Post a Comment