Skip to content

Commit 68cac62

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Simple Array Sum solved ✓
1 parent 09a0fba commit 68cac62

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/warmup/simpleArraySum.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
import (
8+
utils "gon.cl/algorithm-exercises/src/utils"
9+
)
10+
11+
func SimpleArraySum(arr []int) int {
12+
acum := 0
13+
14+
for i := 0; i < len(arr); i++ {
15+
acum += arr[i]
16+
}
17+
18+
utils.Info("SimpleArraySum answer => %d", acum)
19+
20+
return acum
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestSimpleArraySum(t *testing.T) {
11+
12+
expectedSolution := 31
13+
input := []int{1, 2, 3, 4, 10, 11}
14+
15+
testname := fmt.Sprintf("SimpleArraySum(%v) => %d \n", input, expectedSolution)
16+
t.Run(testname, func(t *testing.T) {
17+
18+
ans := SimpleArraySum(input)
19+
assert.Equal(t, expectedSolution, ans)
20+
})
21+
}

0 commit comments

Comments
 (0)