diff --git a/algorithm-exercises-csharp-test/src/hackerrank/warmup/SimpleArraySum.Test.cs b/algorithm-exercises-csharp-test/src/hackerrank/warmup/SimpleArraySum.Test.cs new file mode 100644 index 0000000..cd7d47d --- /dev/null +++ b/algorithm-exercises-csharp-test/src/hackerrank/warmup/SimpleArraySum.Test.cs @@ -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); + } + } +} + diff --git a/algorithm-exercises-csharp/src/hackerrank/warmup/SimpleArraySum.cs b/algorithm-exercises-csharp/src/hackerrank/warmup/SimpleArraySum.cs new file mode 100644 index 0000000..15147ef --- /dev/null +++ b/algorithm-exercises-csharp/src/hackerrank/warmup/SimpleArraySum.cs @@ -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; + } +} 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 $.