Skip to content

Commit de7d288

Browse files
authored
Merge pull request #2 from sir-gon/feature/simple_array_sum
[Hacker Rank]: Warmup: Simple Array Sum. Solved ✅.
2 parents 1e597af + ae7bd28 commit de7d288

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
#pragma once
5+
6+
namespace hackerrank::warmup {
7+
int simpleArraySum(std::vector<int> ar);
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <exercises/hackerrank/warmup/simple_array_sum.hpp>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]]
5+
*/
6+
7+
#include <iostream>
8+
#include <cmath>
9+
#include <vector>
10+
11+
using namespace std;
12+
13+
string ltrim(const string &);
14+
string rtrim(const string &);
15+
vector<string> split(const string &);
16+
17+
18+
namespace hackerrank::warmup {
19+
20+
int simpleArraySum(std::vector<int> ar) {
21+
int accum = 0;
22+
23+
for(const int i : ar) {
24+
accum += i;
25+
}
26+
return accum;
27+
}
28+
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/simple_array_sum.hpp>
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("simpleArraySum", "[warmup]")
13+
{
14+
std::filesystem::path cwd = std::filesystem::current_path();
15+
std::string path = cwd.string() + "/unit/lib/hackerrank/warmup/simple_array_sum.testcase.json";
16+
17+
INFO("simpleArraySum JSON test cases FILE: " << path);
18+
19+
std::ifstream f(path);
20+
json data = json::parse(f);
21+
22+
for (auto testcase : data) {
23+
int result = hackerrank::warmup::simpleArraySum(testcase["input"]);
24+
CHECK(result == testcase["expected"]);
25+
}
26+
}
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)