PHP   65

PHP Hackerrank Challenge

Guest on 24th November 2021 03:48:05 PM

  1. /*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.
  2.  
  3. 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]).
  4.  
  5. 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].
  6.  
  7. If a[i] > b[i], then Alice is awarded 1 point.
  8. If a[i] < b[i], then Bob is awarded 1 point.
  9. If a[i] = b[i], then neither person receives a point.
  10. Comparison points is the total points a person earned.
  11.  
  12. Given a and b, determine their respective comparison points.
  13.  
  14. Example
  15.  
  16. a = [1, 2, 3]
  17. b = [3, 2, 1]
  18. For elements *0*, Bob is awarded a point because a[0] .
  19. For the equal elements a[1] and b[1], no points are earned.
  20. Finally, for elements 2, a[2] > b[2] so Alice receives a point.
  21. The return array is [1, 1] with Alice's score first and Bob's second.
  22.  
  23. Function Description
  24.  
  25. Complete the function compareTriplets in the editor below.
  26.  
  27. compareTriplets has the following parameter(s):
  28.  
  29. int a[3]: Alice's challenge rating
  30. int b[3]: Bob's challenge rating
  31. Return
  32.  
  33. int[2]: Alice's score is in the first position, and Bob's score is in the second.
  34. Input Format
  35.  
  36. The first line contains 3 space-separated integers, a[0], a[1], and a[2], the respective values in triplet a.
  37. The second line contains 3 space-separated integers, b[0], b[1], and b[2], the respective values in triplet b.
  38.  
  39. Constraints
  40.  
  41. 1 ≤ a[i] ≤ 100
  42. 1 ≤ b[i] ≤ 100
  43. Sample Input 0
  44.  
  45. 5 6 7
  46. 3 6 10
  47. Sample Output 0
  48.  
  49. 1 1
  50. Explanation 0
  51.  
  52. In this example:
  53.  
  54. Now, let's compare each individual score:
  55.  
  56. , so Alice receives  point.
  57. , so nobody receives a point.
  58. , so Bob receives  point.
  59. Alice's comparison score is , and Bob's comparison score is . Thus, we return the array .
  60.  
  61. Sample Input 1
  62.  
  63. 17 28 30
  64. 99 16 8
  65. Sample Output 1
  66.  
  67. 2 1
  68. Explanation 1
  69.  
  70. Comparing the  elements,  so Bob receives a point.
  71. Comparing the  and  elements,  and  so Alice receives two points.
  72. The return array is .
  73. */ is// function solution
  74. olution
  75.  
  76. function compareTriplets($a, $b// Write your code here
  77. de here
  78.     $alice = [];
  79.     $billy = [];
  80.    
  81.     for ($i = 0; $i !== count($a); $i++) {
  82.         if ($a[$i] > $b[$i] && $a[$i] !== $b[$i]) {
  83.             array_push($alice, 1);
  84.         } else {
  85.             array_push($billy, 1);
  86.         }
  87.         if ($a[$i] === $b[$i]) {
  88.             break;
  89.         }
  90.     }
  91.  return $points = [array_sum($alice), array_sum($billy// 7 out of 8 testcases
  92. t//expected result
  93.  res// compiler message is wrong answer, could you work out why it may not be as expected?
  94. pected?
  95.  
  96.  
  97. $fptr = fopen(getenv("OUTPUT_PATH"), "w");
  98.  
  99. $a_temp = rtrim(fgets(STDIN));
  100.  
  101. $a = array_map('intval', preg_split('/ /', $a_temp, -1, PREG_SPLIT_NO_EMPTY));
  102.  
  103. $b_temp = rtrim(fgets(STDIN));
  104.  
  105. $b = array_map('intval', preg_split('/ /', $b_temp, -1, PREG_SPLIT_NO_EMPTY));
  106.  
  107. $result = compareTriplets($a, $b);
  108.  
  109. fwrite($fptr, implode(" ", $result) . "\n");
  110.  

Raw Paste


Login or Register to edit or fork this paste. It's free.