Case Study:

Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale  from 1  to 100  for three categories: problem clarity, originality, and difficulty.

We define the rating for Alice’s challenge to be the triplet A = (a0,a1,a2) , and the rating for Bob’s challenge to be the triplet B = (b0,b1,b2).

Your task is to find their comparison points by comparing with a0, with b0,a1 with b1, and a2 with b2.

  • If ai > bi, then Alice is awarded point.
  • If  ai < bi, then Bob is awarded point.
  • If ai = bi, then neither person receives a point.

Comparison points is the total points a person earned.

Given A and B, can you compare the two challenges and print their respective comparison points?

Input Format

The first line contains 3 space-separated integers, a0,a1 , and a2, describing the respective values in triplet A.
The second line contains space-separated integers, b0, b1, and b2, describing the respective values in triplet B.

Constraints

 1 <= ai <= 100
1 <= bi <= 100

Output Format

Print two space-separated integers denoting the respective comparison points earned by Alice and Bob.

Sample Input

5 6 7
3 6 10

Sample Output

1 1 

Explanation

In this example:

Now, let’s compare each individual score:

  • a0 > b0, so Alice receives point.
  • a1 = b1, so nobody receives a point.
  • a2 < b2, so Bob receives point.

Alice’s comparison score is 1, and Bob’s comparison score is 1. Thus, we print 1 1 (Alice’s comparison score followed by Bob’s comparison score) on a single line.

Solution

Link to github

<?php

$handle = fopen ("php://stdin", "r");
function solve($a0, $a1, $a2, $b0, $b1, $b2){
    // Complete this function
    $aScore = 0;
    $bScore = 0;

    $aScore += ($a0 > $b0? 1:0) + ($a1 > $b1? 1:0) + ($a2 > $b2? 1:0);
    $bScore += ($b0 > $a0? 1:0) + ($b1 > $a1? 1:0) + ($b2 > $a2? 1:0);
    
    $score = [];
    array_push($score,$aScore,$bScore);
    return $score;   
}

fscanf($handle, "%d %d %d", $a0, $a1, $a2);
fscanf($handle, "%d %d %d", $b0, $b1, $b2);
$result = solve($a0, $a1, $a2, $b0, $b1, $b2);
echo implode(" ", $result)."\n";
?>

Feel free to add your comments.

One Reply to “PHP: Compare the Triplets Algorithms”

  1. I am doing it the following way but I am still getting incorrect output for some reason. Could you please advice?

    // Complete the compareTriplets function below.
    function compareTriplets($a, $b) {

    $totalA = 0;
    $totalB = 0;
    $score = [];
    array_push($score,$totalA,$totalB);

    //case 1

    if($a[0]>$b[0]){
    echo $totalA += 1;
    }elseif($a[0]$b[1]){
    echo $totalA += 1;
    }elseif($a[1]$b[2]){
    echo $totalA += 1;
    }elseif($a[2]<$b[2]){
    echo $totalB += 1;
    }else {
    echo $totalA += 0;
    echo $totalB += 0;
    }

    return $score;

    }

Leave a Reply

Your email address will not be published. Required fields are marked *