diff --git a/docs/hackerrank/warmup/simple_array_sum.md b/docs/hackerrank/warmup/simple_array_sum.md new file mode 100644 index 0000000..7cd4e53 --- /dev/null +++ b/docs/hackerrank/warmup/simple_array_sum.md @@ -0,0 +1,45 @@ +# [Simple Array Sum](https://www.hackerrank.com/challenges/simple-array-sum/problem) + +Difficulty: #easy +Category: #warmup + +Given an array of integers, find the sum of its elements. +For example, if the array $ ar = [1, 2, 3], 1 + 2 + 3 = 6 $, so return $ 6 $. + +## Function Description + +Complete the simpleArraySum function in the editor below. It must return the sum + of the array elements as an integer. +simpleArraySum has the following parameter(s): + +- ar: an array of integers + +## Input Format + +The first line contains an integer, , denoting the size of the array. +The second line contains space-separated integers representing the array's elements. + +## Constraints + +$ 0 < n, ar[i] \leq 1000 $ + +## Output Format + +Print the sum of the array's elements as a single integer. + +## Sample Input + +```text +6 +1 2 3 4 10 11 +``` + +## Sample Output + +```text +31 +``` + +## Explanation + +We print the sum of the array's elements: $ 1 + 2 + 3 + 4 + 10 + 11 = 31 $. diff --git a/src/lib/exercises/include/exercises/hackerrank/warmup/simple_array_sum.hpp b/src/lib/exercises/include/exercises/hackerrank/warmup/simple_array_sum.hpp new file mode 100644 index 0000000..6dd9072 --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/warmup/simple_array_sum.hpp @@ -0,0 +1,8 @@ +#include +#include + +#pragma once + +namespace hackerrank::warmup { + int simpleArraySum(std::vector ar); +} diff --git a/src/lib/exercises/src/hackerrank/warmup/simple_array_sum.cpp b/src/lib/exercises/src/hackerrank/warmup/simple_array_sum.cpp new file mode 100644 index 0000000..19cb96a --- /dev/null +++ b/src/lib/exercises/src/hackerrank/warmup/simple_array_sum.cpp @@ -0,0 +1,29 @@ +#include + +/** + * @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]] + */ + +#include +#include +#include + +using namespace std; + +string ltrim(const string &); +string rtrim(const string &); +vector split(const string &); + + +namespace hackerrank::warmup { + + int simpleArraySum(std::vector ar) { + int accum = 0; + + for(const int i : ar) { + accum += i; + } + return accum; + } + +} diff --git a/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.test.cpp b/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.test.cpp new file mode 100644 index 0000000..c389d86 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.test.cpp @@ -0,0 +1,26 @@ +#include + +#include +#include +#include + +#include +#include +#include +using json = nlohmann::json; + +TEST_CASE("simpleArraySum", "[warmup]") +{ + std::filesystem::path cwd = std::filesystem::current_path(); + std::string path = cwd.string() + "/unit/lib/hackerrank/warmup/simple_array_sum.testcase.json"; + + INFO("simpleArraySum JSON test cases FILE: " << path); + + std::ifstream f(path); + json data = json::parse(f); + + for (auto testcase : data) { + int result = hackerrank::warmup::simpleArraySum(testcase["input"]); + CHECK(result == testcase["expected"]); + } +} diff --git a/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.testcase.json b/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.testcase.json new file mode 100644 index 0000000..c916b56 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/simple_array_sum.testcase.json @@ -0,0 +1,6 @@ +[ + { + "input": [1, 2, 3, 4, 10, 11], + "expected": 31 + } +]