Skip to content

[Hacker Rank]: Simple Array Sum solved ✓ #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace algorithm_exercises_csharp;

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class SimpleArraySumTest
{
public class SimpleArraySumTestCase
{
public int[] inputs = [];
public int expected = 0;
}

// dotnet_style_readonly_field = true
private static readonly SimpleArraySumTestCase[] tests = [
new() { inputs = [1, 2, 3, 4, 10, 11], expected = 31 }
];

[TestMethod]
public void testSimpleArraySum()
{
int? result;

foreach (SimpleArraySumTestCase test in tests)
{
result = SimpleArraySum.simpleArraySum(test.inputs);
Assert.AreEqual(test.expected, result);
}
}
}

23 changes: 23 additions & 0 deletions algorithm-exercises-csharp/src/hackerrank/warmup/SimpleArraySum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]]

namespace algorithm_exercises_csharp;

using System.Diagnostics.CodeAnalysis;

public class SimpleArraySum
{
[ExcludeFromCodeCoverage]
protected SimpleArraySum() { }

public static int simpleArraySum(int[] inputs)
{
var total = 0;

foreach (int i in inputs)
{
total += i;
}

return total;
}
}
45 changes: 45 additions & 0 deletions docs/hackerrank/warmup/simple_array_sum.md
Original file line number Diff line number Diff line change
@@ -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 $.