Skip to content

Commit 0e8edfc

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Simple Array Sum solved ✓
1 parent 19e0344 commit 0e8edfc

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace algorithm_exercises_csharp;
2+
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
[TestClass]
6+
public class SimpleArraySumTest
7+
{
8+
9+
protected class SimpleArraySumTestCase
10+
{
11+
public int[] inputs = [];
12+
public int expected = 0;
13+
}
14+
15+
[TestMethod]
16+
public void TestSimpleArraySum(int[] input, int expected)
17+
{
18+
19+
20+
int result = SimpleArraySum.simpleArraySum(input);
21+
22+
Assert.AreEqual(expected, result);
23+
}
24+
25+
// [TestMethod]
26+
// public void TestSimpleArraySum()
27+
// {
28+
// int expected = 5;
29+
// int[] input = [2, 3];
30+
// int result = SimpleArraySum.simpleArraySum(input);
31+
32+
// Assert.AreEqual(expected, result);
33+
// }
34+
35+
}
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]]
2+
3+
namespace algorithm_exercises_csharp;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public class SimpleArraySum
8+
{
9+
[ExcludeFromCodeCoverage]
10+
protected SimpleArraySum() { }
11+
12+
public static int simpleArraySum(int[] inputs)
13+
{
14+
var total = 0;
15+
16+
foreach (int i in inputs)
17+
{
18+
total += i;
19+
}
20+
21+
return total;
22+
}
23+
}
+45
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 $.

0 commit comments

Comments
 (0)