From 3fd7dffab6124663505afae3a884486cddaf4a32 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Tue, 29 Oct 2024 10:52:16 -0300 Subject: [PATCH 1/2] =?UTF-8?q?[Hacker=20Rank]:=20Warmup:=20Compare=20trip?= =?UTF-8?q?lets.=20Solved=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/hackerrank/warmup/compare_triplets.md | 104 ++++++++++++++++++ .../hackerrank/warmup/compare_triplets.h | 12 ++ .../src/hackerrank/warmup/compare_triplets.c | 25 +++++ .../warmup/compare_triplets.test.cpp | 42 +++++++ .../warmup/compare_triplets.testcases.json | 3 + 5 files changed, 186 insertions(+) create mode 100644 docs/hackerrank/warmup/compare_triplets.md create mode 100644 src/lib/exercises/include/exercises/hackerrank/warmup/compare_triplets.h create mode 100644 src/lib/exercises/src/hackerrank/warmup/compare_triplets.c create mode 100644 src/tests/unit/lib/hackerrank/warmup/compare_triplets.test.cpp create mode 100644 src/tests/unit/lib/hackerrank/warmup/compare_triplets.testcases.json diff --git a/docs/hackerrank/warmup/compare_triplets.md b/docs/hackerrank/warmup/compare_triplets.md new file mode 100644 index 0000000..e87c789 --- /dev/null +++ b/docs/hackerrank/warmup/compare_triplets.md @@ -0,0 +1,104 @@ +# [Compare the Triplets](https://www.hackerrank.com/challenges/compare-the-triplets) + +Difficulty: #easy +Category: #warmup + +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. +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 is the total points a person earned. +Given a and b, determine their respective comparison points. + +## Example + +$ a = [1, 2, 3] $ \ +$ b = [3, 2, 1] $ + +- For elements \*0\*, Bob is awarded a point because $ a[0] $. +- For the equal elements $ a[1] $ and $ b[1] $, no points are earned. +- Finally, for elements $ 2 $, $ a[2] > b[2] $ so Alice receives a point. + +The return array is $ [1, 1] $ with Alice's score first and Bob's second. + +## Function Description + +Complete the function compareTriplets in the editor below. +compareTriplets has the following parameter(s): + +- int a[3]: Alice's challenge rating +- int b[3]: Bob's challenge rating + +## Return + +- int[2]: Alice's score is in the first position, and Bob's score is in the second. + +## Input Format + +The first line contains 3 space-separated integers, $ a[0] $, $ a[1] $, and +$ a[2] $, the respective values in triplet a. +The second line contains 3 space-separated integers, $ b[0] $, $ b[1] $, and +$ b[2] $, the respective values in triplet b. + +## Constraints + +- $ 1 \leq a[i] \leq 100 $ +- $ 1 \leq b[i] \leq 100 $ + +## Sample Input 0 + +```text +5 6 7 +3 6 10 +``` + +## Sample Output 0 + +```text +1 1 +``` + +## Explanation 0 + +In this example: + +- $ a = (a[0], a[1], a[2]) = (5, 6, 7) $ +- $ b = (b[0], b[1], b[2]) = (3, 6, 10) $ + +Now, let's compare each individual score: + +- $ a[0] > b[0] $, so Alice receives $ 1 $ point. \ +- $ a[1] = b[1] $, so nobody receives a point. \ +- $ a[2] < b[2] $, so Bob receives $ 1 $ point. + +Alice's comparison score is $ 1 $, and Bob's comparison score is $ 1 $. +Thus, we return the array $ [1, 1] $. + +## Sample Input 1 + +```text +17 28 30 +99 16 8 +``` + +## Sample Output 1 + +```text +2 1 +``` + +## Explanation 1 + +Comparing the *0th* elements, $ 17 < 99 $ so Bob receives a point. +Comparing the *1st* and *2nd* elements $ 28 > 16 $ and $ 30 > 8 $ so Alice + receives two points. +The return array is $ [2, 1] $. diff --git a/src/lib/exercises/include/exercises/hackerrank/warmup/compare_triplets.h b/src/lib/exercises/include/exercises/hackerrank/warmup/compare_triplets.h new file mode 100644 index 0000000..e5f163c --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/warmup/compare_triplets.h @@ -0,0 +1,12 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +int *HACKERRANK_WARMUP_compareTriplets(int a_count, const int *a, int b_count, + const int *b, int *result_count); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/src/lib/exercises/src/hackerrank/warmup/compare_triplets.c b/src/lib/exercises/src/hackerrank/warmup/compare_triplets.c new file mode 100644 index 0000000..bbfcaf5 --- /dev/null +++ b/src/lib/exercises/src/hackerrank/warmup/compare_triplets.c @@ -0,0 +1,25 @@ +#include + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +/** + * @link Problem definition [[docs/hackerrank/warmup/compare_triplets.md]] + */ + +int* HACKERRANK_WARMUP_compareTriplets(int a_count, const int *a, int b_count, + const int *b, int *result_count) { + + *result_count = 2; + static int awards[2] = {0, 0}; + + for (int i = 0; i < MIN(a_count, b_count); i++) { + if (a[i] > b[i]) { + awards[0] = awards[0] + 1; + } + if (a[i] < b[i]) { + awards[1] = awards[1] + 1; + } + } + + return awards; +} diff --git a/src/tests/unit/lib/hackerrank/warmup/compare_triplets.test.cpp b/src/tests/unit/lib/hackerrank/warmup/compare_triplets.test.cpp new file mode 100644 index 0000000..03316a2 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/compare_triplets.test.cpp @@ -0,0 +1,42 @@ +#include + +#include +#include +#include + +#include +#include +#include +using json = nlohmann::json; + +TEST_CASE("compareTriplets JSON Test Cases", + "[hackerrank] [jsontestcase] [warmup]") { + std::filesystem::path cwd = std::filesystem::current_path(); + std::string path = + cwd.string() + + "/unit/lib/hackerrank/warmup/compare_triplets.testcases.json"; + + INFO("compareTriplets JSON test cases FILE: " << path); + + std::ifstream f(path); + json data = json::parse(f); + + for (auto testcase : data) { + auto a_size = static_cast(testcase["a"].size()); + std::vector a_input_vector = testcase["a"]; + const int *a_input_array = a_input_vector.data(); + + auto b_size = static_cast(testcase["b"].size()); + std::vector b_input_vector = testcase["b"]; + const int *b_input_array = b_input_vector.data(); + int result_count; + + int *result = HACKERRANK_WARMUP_compareTriplets( + a_size, a_input_array, b_size, b_input_array, &result_count); + + // Crear un vector a partir del array en C + std::vector result_as_vector(result, result + result_count); + + CHECK(result_as_vector == testcase["expected"]); + } +} diff --git a/src/tests/unit/lib/hackerrank/warmup/compare_triplets.testcases.json b/src/tests/unit/lib/hackerrank/warmup/compare_triplets.testcases.json new file mode 100644 index 0000000..f4b2862 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/compare_triplets.testcases.json @@ -0,0 +1,3 @@ +[ + { "a": [5, 6, 7], "b": [3, 6, 10], "expected": [1, 1] } +] From 2019b09b1cd79871b5997d3fb02011d95e02a0d9 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Tue, 29 Oct 2024 16:13:51 -0300 Subject: [PATCH 2/2] =?UTF-8?q?[Hacker=20Rank]:=20Warmup:=20Compare=20trip?= =?UTF-8?q?lets.=20Solved=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/exercises/include/exercises/basic/common.h | 4 ++++ .../src/hackerrank/warmup/compare_triplets.c | 12 ++++++++---- .../lib/hackerrank/warmup/compare_triplets.test.cpp | 2 ++ .../warmup/compare_triplets.testcases.json | 5 ++++- 4 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 src/lib/exercises/include/exercises/basic/common.h diff --git a/src/lib/exercises/include/exercises/basic/common.h b/src/lib/exercises/include/exercises/basic/common.h new file mode 100644 index 0000000..4ec5550 --- /dev/null +++ b/src/lib/exercises/include/exercises/basic/common.h @@ -0,0 +1,4 @@ +#pragma once + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) diff --git a/src/lib/exercises/src/hackerrank/warmup/compare_triplets.c b/src/lib/exercises/src/hackerrank/warmup/compare_triplets.c index bbfcaf5..3cbfb53 100644 --- a/src/lib/exercises/src/hackerrank/warmup/compare_triplets.c +++ b/src/lib/exercises/src/hackerrank/warmup/compare_triplets.c @@ -1,16 +1,20 @@ -#include +#include -#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#include +#include /** * @link Problem definition [[docs/hackerrank/warmup/compare_triplets.md]] */ -int* HACKERRANK_WARMUP_compareTriplets(int a_count, const int *a, int b_count, +int *HACKERRANK_WARMUP_compareTriplets(int a_count, const int *a, int b_count, const int *b, int *result_count) { *result_count = 2; - static int awards[2] = {0, 0}; + int *awards = malloc(sizeof(int) * *result_count); + + awards[0] = 0; + awards[1] = 0; for (int i = 0; i < MIN(a_count, b_count); i++) { if (a[i] > b[i]) { diff --git a/src/tests/unit/lib/hackerrank/warmup/compare_triplets.test.cpp b/src/tests/unit/lib/hackerrank/warmup/compare_triplets.test.cpp index 03316a2..4174ad5 100644 --- a/src/tests/unit/lib/hackerrank/warmup/compare_triplets.test.cpp +++ b/src/tests/unit/lib/hackerrank/warmup/compare_triplets.test.cpp @@ -38,5 +38,7 @@ TEST_CASE("compareTriplets JSON Test Cases", std::vector result_as_vector(result, result + result_count); CHECK(result_as_vector == testcase["expected"]); + + free(result); } } diff --git a/src/tests/unit/lib/hackerrank/warmup/compare_triplets.testcases.json b/src/tests/unit/lib/hackerrank/warmup/compare_triplets.testcases.json index f4b2862..697e894 100644 --- a/src/tests/unit/lib/hackerrank/warmup/compare_triplets.testcases.json +++ b/src/tests/unit/lib/hackerrank/warmup/compare_triplets.testcases.json @@ -1,3 +1,6 @@ [ - { "a": [5, 6, 7], "b": [3, 6, 10], "expected": [1, 1] } + { "a": [5, 6, 7], "b": [3, 6, 10], "expected": [1, 1] }, + { "a": [], "b": [], "expected": [0, 0] }, + { "a": [1], "b": [], "expected": [0, 0] }, + { "a": [], "b": [1], "expected": [0, 0] } ]