Skip to content

Commit bf5341f

Browse files
authored
Merge pull request #8 from sir-gon/compare_triplets
Compare triplets
2 parents 225b6d5 + 2019b09 commit bf5341f

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# [Compare the Triplets](https://www.hackerrank.com/challenges/compare-the-triplets)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Alice and Bob each created one problem for HackerRank. A reviewer rates the two
7+
challenges, awarding points on a scale from 1 to 100 for three categories:
8+
problem clarity, originality, and difficulty.
9+
The rating for Alice's challenge is the triplet $ a = (a[0], a[1], a[2]) $,
10+
and the rating for Bob's challenge is the triplet $ b = (b[0], b[1], b[2]) $.
11+
12+
The task is to find their comparison points by comparing $ a[0] $ with
13+
$ b[0] $, $ a[1] $ with $ b[1] $, and $ a[2] $ with $ b[2] $.
14+
15+
- If $ a[i] > b[i] $, then Alice is awarded $ 1 $ point.
16+
- If $ a[i] < b[i] $, then Bob is awarded $ 1 $ point.
17+
- If $ a[i] = b[i] $, then neither person receives a point.
18+
19+
Comparison points is the total points a person earned.
20+
Given a and b, determine their respective comparison points.
21+
22+
## Example
23+
24+
$ a = [1, 2, 3] $ \
25+
$ b = [3, 2, 1] $
26+
27+
- For elements \*0\*, Bob is awarded a point because $ a[0] $.
28+
- For the equal elements $ a[1] $ and $ b[1] $, no points are earned.
29+
- Finally, for elements $ 2 $, $ a[2] > b[2] $ so Alice receives a point.
30+
31+
The return array is $ [1, 1] $ with Alice's score first and Bob's second.
32+
33+
## Function Description
34+
35+
Complete the function compareTriplets in the editor below.
36+
compareTriplets has the following parameter(s):
37+
38+
- int a[3]: Alice's challenge rating
39+
- int b[3]: Bob's challenge rating
40+
41+
## Return
42+
43+
- int[2]: Alice's score is in the first position, and Bob's score is in the second.
44+
45+
## Input Format
46+
47+
The first line contains 3 space-separated integers, $ a[0] $, $ a[1] $, and
48+
$ a[2] $, the respective values in triplet a.
49+
The second line contains 3 space-separated integers, $ b[0] $, $ b[1] $, and
50+
$ b[2] $, the respective values in triplet b.
51+
52+
## Constraints
53+
54+
- $ 1 \leq a[i] \leq 100 $
55+
- $ 1 \leq b[i] \leq 100 $
56+
57+
## Sample Input 0
58+
59+
```text
60+
5 6 7
61+
3 6 10
62+
```
63+
64+
## Sample Output 0
65+
66+
```text
67+
1 1
68+
```
69+
70+
## Explanation 0
71+
72+
In this example:
73+
74+
- $ a = (a[0], a[1], a[2]) = (5, 6, 7) $
75+
- $ b = (b[0], b[1], b[2]) = (3, 6, 10) $
76+
77+
Now, let's compare each individual score:
78+
79+
- $ a[0] > b[0] $, so Alice receives $ 1 $ point. \
80+
- $ a[1] = b[1] $, so nobody receives a point. \
81+
- $ a[2] < b[2] $, so Bob receives $ 1 $ point.
82+
83+
Alice's comparison score is $ 1 $, and Bob's comparison score is $ 1 $.
84+
Thus, we return the array $ [1, 1] $.
85+
86+
## Sample Input 1
87+
88+
```text
89+
17 28 30
90+
99 16 8
91+
```
92+
93+
## Sample Output 1
94+
95+
```text
96+
2 1
97+
```
98+
99+
## Explanation 1
100+
101+
Comparing the *0th* elements, $ 17 < 99 $ so Bob receives a point.
102+
Comparing the *1st* and *2nd* elements $ 28 > 16 $ and $ 30 > 8 $ so Alice
103+
receives two points.
104+
The return array is $ [2, 1] $.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
#define MIN(a, b) ((a) < (b) ? (a) : (b))
4+
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
int *HACKERRANK_WARMUP_compareTriplets(int a_count, const int *a, int b_count,
8+
const int *b, int *result_count);
9+
10+
#ifdef __cplusplus
11+
} // extern "C"
12+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdlib.h>
2+
3+
#include <exercises/basic/common.h>
4+
#include <exercises/hackerrank/warmup/compare_triplets.h>
5+
6+
/**
7+
* @link Problem definition [[docs/hackerrank/warmup/compare_triplets.md]]
8+
*/
9+
10+
int *HACKERRANK_WARMUP_compareTriplets(int a_count, const int *a, int b_count,
11+
const int *b, int *result_count) {
12+
13+
*result_count = 2;
14+
int *awards = malloc(sizeof(int) * *result_count);
15+
16+
awards[0] = 0;
17+
awards[1] = 0;
18+
19+
for (int i = 0; i < MIN(a_count, b_count); i++) {
20+
if (a[i] > b[i]) {
21+
awards[0] = awards[0] + 1;
22+
}
23+
if (a[i] < b[i]) {
24+
awards[1] = awards[1] + 1;
25+
}
26+
}
27+
28+
return awards;
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/compare_triplets.h>
4+
#include <iostream>
5+
#include <vector>
6+
7+
#include <filesystem>
8+
#include <fstream>
9+
#include <nlohmann/json.hpp>
10+
using json = nlohmann::json;
11+
12+
TEST_CASE("compareTriplets JSON Test Cases",
13+
"[hackerrank] [jsontestcase] [warmup]") {
14+
std::filesystem::path cwd = std::filesystem::current_path();
15+
std::string path =
16+
cwd.string() +
17+
"/unit/lib/hackerrank/warmup/compare_triplets.testcases.json";
18+
19+
INFO("compareTriplets JSON test cases FILE: " << path);
20+
21+
std::ifstream f(path);
22+
json data = json::parse(f);
23+
24+
for (auto testcase : data) {
25+
auto a_size = static_cast<int>(testcase["a"].size());
26+
std::vector<int> a_input_vector = testcase["a"];
27+
const int *a_input_array = a_input_vector.data();
28+
29+
auto b_size = static_cast<int>(testcase["b"].size());
30+
std::vector<int> b_input_vector = testcase["b"];
31+
const int *b_input_array = b_input_vector.data();
32+
int result_count;
33+
34+
int *result = HACKERRANK_WARMUP_compareTriplets(
35+
a_size, a_input_array, b_size, b_input_array, &result_count);
36+
37+
// Crear un vector a partir del array en C
38+
std::vector<int> result_as_vector(result, result + result_count);
39+
40+
CHECK(result_as_vector == testcase["expected"]);
41+
42+
free(result);
43+
}
44+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{ "a": [5, 6, 7], "b": [3, 6, 10], "expected": [1, 1] },
3+
{ "a": [], "b": [], "expected": [0, 0] },
4+
{ "a": [1], "b": [], "expected": [0, 0] },
5+
{ "a": [], "b": [1], "expected": [0, 0] }
6+
]

0 commit comments

Comments
 (0)