Compare the Triplets is an easy-level Python problem that requires basic knowledge of Python. In this post, we will provide a Python solution for Compare the Triplets.
Problem Statement and Explanation
Alice and Bob each submitted a problem to HackerRank. A reviewer evaluated the challenges on three categories - problem clarity, originality, and difficulty - using a scale of 1 to 100.
The rating for Alice’s challenge is the triplet a
= (a[0], a[1], a[2])
, and the rating for Bob’s challenge is the triplet b
= (b[0], b[1], b[2])
.
The 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 1 point. - If
a[i]
<b[i]
, then Bob is awarded 1 point. - If
a[i]
=b[i]
, then neither person receives a point.
Comparison points are the total points a person earned.
Given a and b, determine their respective comparison points.
Input Format
- The first line contains 3 space-separated integers, a[0], a[1], and a[2], describing the respective values in triplet a.
- The second line contains 3 space-separated integers, b[0], b[1], and b[2], describing the respective values in triplet b.
Output Format
- Return an array of two integers denoting the respective comparison points earned by Alice and Bob.
Solve Me First Solution in Python
Explanation of Solution in Python
The function called compareTriplets()
that takes two lists as input, a
and b
. The function compares the corresponding elements of a
and b
and keeps a running tally of the number of times each person scores a point. The function then returns a list with the scores for Alice and Bob.
- The function takes two lists as input, but they must have the same length.
- The function uses a for loop to iterate over the elements of the two lists.
- The function uses the if statement to compare the corresponding elements of the two lists.
- The function uses the return statement to return the scores for Alice and Bob.
Solve Me First Solution in C++
Explanation of Solution in C++
The function first declares two variables, aliceScore
and bobScore
, to keep track of the scores for Alice and Bob. Then, it loops through the elements of the two lists, comparing the corresponding elements. If the element in a
is greater than the corresponding element in b
, then aliceScore
is incremented. Otherwise, bobScore
is incremented.
After the loop, the function pushes the scores for Alice and Bob onto a vector and then returns the vector.
Top comments (0)