From 66749b1f685014a6da4131fff54d0ef849978bf3 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Mon, 28 Oct 2024 16:37:06 -0300 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]:=20Warmup:=20A=20Very=20Big=20S?= =?UTF-8?q?um.=20Solved=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/hackerrank/warmup/a_very_big_sum.md | 55 +++++++++++++++++++ .../hackerrank/warmup/a_very_big_sum.h | 11 ++++ .../src/hackerrank/warmup/a_very_big_sum.c | 15 +++++ .../hackerrank/warmup/a_very_big_sum.test.cpp | 32 +++++++++++ .../warmup/a_very_big_sum.testcases.json | 6 ++ 5 files changed, 119 insertions(+) create mode 100644 docs/hackerrank/warmup/a_very_big_sum.md create mode 100644 src/lib/exercises/include/exercises/hackerrank/warmup/a_very_big_sum.h create mode 100644 src/lib/exercises/src/hackerrank/warmup/a_very_big_sum.c create mode 100644 src/tests/unit/lib/hackerrank/warmup/a_very_big_sum.test.cpp create mode 100644 src/tests/unit/lib/hackerrank/warmup/a_very_big_sum.testcases.json diff --git a/docs/hackerrank/warmup/a_very_big_sum.md b/docs/hackerrank/warmup/a_very_big_sum.md new file mode 100644 index 0000000..7623241 --- /dev/null +++ b/docs/hackerrank/warmup/a_very_big_sum.md @@ -0,0 +1,55 @@ +# [A Very Big Sum](https://www.hackerrank.com/challenges/a-very-big-sum) + +Difficulty: #easy +Category: #warmup + +In this challenge, you are required to calculate and print the +sum of the elements in an array, keeping in mind that some of +those integers may be quite large. + +## Function Description + +Complete the aVeryBigSum function in the editor below. +It must return the sum of all array elements. + +aVeryBigSum has the following parameter(s): + +- int ar[n]: an array of integers. + +## Return + +- long: the sum of all array elements + +## Input Format + +The first line of the input consists of an integer n. +The next line contains space-separated integers contained in the array. + +## Output Format + +Return the integer sum of the elements in the array. + +## Constraints + +$ 1 <= n < 10 $ \ +$ 0 <= ar[i] <= 10^10 $ + +## Sample Input + +```text +5 +1000000001 1000000002 1000000003 1000000004 1000000005 +``` + +## Output + +```text +5000000015 +``` + +## Note + +The range of the 32-bit integer is +($ -2^31 $) to ($ 2^31 - 1 $) or $ [-2147483648, 2147483647] $ +When we add several integer values, the resulting sum might exceed the +above range. You might need to use long int C/C++/Java to store such sums. diff --git a/src/lib/exercises/include/exercises/hackerrank/warmup/a_very_big_sum.h b/src/lib/exercises/include/exercises/hackerrank/warmup/a_very_big_sum.h new file mode 100644 index 0000000..0f36726 --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/warmup/a_very_big_sum.h @@ -0,0 +1,11 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +long HACKERRANK_WARMUP_aVeryBigSum(int ar_count, const long *ar); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/src/lib/exercises/src/hackerrank/warmup/a_very_big_sum.c b/src/lib/exercises/src/hackerrank/warmup/a_very_big_sum.c new file mode 100644 index 0000000..821109e --- /dev/null +++ b/src/lib/exercises/src/hackerrank/warmup/a_very_big_sum.c @@ -0,0 +1,15 @@ +#include + +/** + * @link Problem definition [[docs/hackerrank/warmup/a_very_big_sum.md]] + */ + +long HACKERRANK_WARMUP_aVeryBigSum(int ar_count, const long *ar) { + long total = 0; + + for (int i = 0; i < ar_count; i++) { + total += ar[i]; + } + + return total; +} diff --git a/src/tests/unit/lib/hackerrank/warmup/a_very_big_sum.test.cpp b/src/tests/unit/lib/hackerrank/warmup/a_very_big_sum.test.cpp new file mode 100644 index 0000000..f90a329 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/a_very_big_sum.test.cpp @@ -0,0 +1,32 @@ +#include + +#include +#include +#include + +#include +#include +#include +using json = nlohmann::json; + +TEST_CASE("aVeryBigSum JSON Test Cases", + "[hackerrank] [jsontestcase] [warmup]") { + std::filesystem::path cwd = std::filesystem::current_path(); + std::string path = + cwd.string() + + "/unit/lib/hackerrank/warmup/a_very_big_sum.testcases.json"; + + INFO("aVeryBigSum JSON test cases FILE: " << path); + + std::ifstream f(path); + json data = json::parse(f); + + for (auto testcase : data) { + auto size = static_cast(testcase["input"].size()); + std::vector input_vector = testcase["input"]; + const long *input_array = input_vector.data(); + + long result = HACKERRANK_WARMUP_aVeryBigSum(size, input_array); + CHECK(result == testcase["expected"]); + } +} diff --git a/src/tests/unit/lib/hackerrank/warmup/a_very_big_sum.testcases.json b/src/tests/unit/lib/hackerrank/warmup/a_very_big_sum.testcases.json new file mode 100644 index 0000000..0dde275 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/a_very_big_sum.testcases.json @@ -0,0 +1,6 @@ +[ + { + "input": [1000000001, 1000000002, 1000000003, 1000000004, 1000000005], + "expected": 5000000015 + } +]