Skip to content

Commit a4f8e2e

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Warmup: Simple Array Sum. Solved ✅.
1 parent 6378685 commit a4f8e2e

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-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 $.

src/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod solve_me_first;
2+
pub mod simple_array_sum;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]]
2+
3+
pub fn simple_array_sum(ar: &[i32]) -> i32 {
4+
ar.iter().sum()
5+
}
6+
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+
]

tests/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod solve_me_first;
2+
pub mod simple_array_sum;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use exercises::hackerrank::warmup::simple_array_sum::simple_array_sum;
2+
use once_cell::sync::Lazy;
3+
use serde::Deserialize;
4+
5+
use crate::common;
6+
use common::utils::load_json;
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use super::*;
11+
12+
#[derive(Debug, Deserialize)]
13+
struct SimpleArraySumTestCase {
14+
input: Vec<i32>,
15+
expected: i32
16+
}
17+
18+
static TEST_DATA: Lazy<Vec<SimpleArraySumTestCase>> =
19+
Lazy::new(|| load_json("tests/data/hackerrank/warmup/simple_array_sum.testcases.json"));
20+
21+
#[test]
22+
fn test_simple_array_sum() {
23+
println!("Testing hackerrank::warmup::simple_array_sum::simple_array_sum()");
24+
25+
for test_case in TEST_DATA.iter() {
26+
let slice: &[i32] = &test_case.input;
27+
let result = simple_array_sum(slice);
28+
assert_eq!(result, test_case.expected);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)