Skip to content

Commit 3669303

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Warmup: Simple Array Sum. Solved ✅.
1 parent d7a451e commit 3669303

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [Simple Array Sum](https://www.hackerrank.com/challenges/simple-array-sum/problem)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Given an array of integers, find the sum of its elements.
7+
For example, if the array $ ar = [1, 2, 3], 1 + 2 + 3 = 6 $, so return $ 6 $.
8+
9+
## Function Description
10+
11+
Complete the simpleArraySum function in the editor below. It must return the sum
12+
of the array elements as an integer.
13+
simpleArraySum has the following parameter(s):
14+
15+
- ar: an array of integers
16+
17+
## Input Format
18+
19+
The first line contains an integer, , denoting the size of the array.
20+
The second line contains space-separated integers representing the array's elements.
21+
22+
## Constraints
23+
24+
$ 0 < n, ar[i] \leq 1000 $
25+
26+
## Output Format
27+
28+
Print the sum of the array's elements as a single integer.
29+
30+
## Sample Input
31+
32+
```text
33+
6
34+
1 2 3 4 10 11
35+
```
36+
37+
## Sample Output
38+
39+
```text
40+
31
41+
```
42+
43+
## Explanation
44+
45+
We print the sum of the array's elements: $ 1 + 2 + 3 + 4 + 10 + 11 = 31 $.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
int HACKERRANK_WARMUP_simpleArraySum(int ar_count, const int *ar);
8+
9+
#ifdef __cplusplus
10+
} // extern "C"
11+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <exercises/hackerrank/warmup/simple_array_sum.h>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]]
5+
*/
6+
7+
int HACKERRANK_WARMUP_simpleArraySum(int ar_count, const int *ar) {
8+
int total = 0;
9+
10+
for (int i = 0; i < ar_count; i++) {
11+
total += ar[i];
12+
}
13+
14+
return total;
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/simple_array_sum.h>
4+
#include <filesystem>
5+
#include <fstream>
6+
#include <nlohmann/json.hpp>
7+
#include <vector>
8+
9+
using json = nlohmann::json;
10+
11+
TEST_CASE("simpleArraySum JSON Test Cases",
12+
"[hackerrank] [jsontestcase] [warmup]") {
13+
std::filesystem::path cwd = std::filesystem::current_path();
14+
std::string path =
15+
cwd.string() +
16+
"/unit/lib/hackerrank/warmup/simple_array_sum.testcases.json";
17+
18+
INFO("simpleArraySum JSON test cases FILE: " << path);
19+
20+
std::ifstream f(path);
21+
json data = json::parse(f);
22+
23+
for (auto testcase : data) {
24+
auto size = static_cast<int>(testcase["input"].size());
25+
std::vector<int> input_vector = testcase["input"];
26+
const int *input_array = input_vector.data();
27+
28+
int result = HACKERRANK_WARMUP_simpleArraySum(size, input_array);
29+
CHECK(result == testcase["expected"]);
30+
}
31+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"input": [1, 2, 3, 4, 10, 11],
4+
"expected": 31
5+
}
6+
]

0 commit comments

Comments
 (0)