Skip to content
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
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 $.
1 change: 1 addition & 0 deletions src/hackerrank/warmup/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod solve_me_first;
pub mod simple_array_sum;
6 changes: 6 additions & 0 deletions src/hackerrank/warmup/simple_array_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]]

pub fn simple_array_sum(ar: &[i32]) -> i32 {
ar.iter().sum()
}

6 changes: 6 additions & 0 deletions tests/data/hackerrank/warmup/simple_array_sum.testcases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"input": [1, 2, 3, 4, 10, 11],
"expected": 31
}
]
1 change: 1 addition & 0 deletions tests/hackerrank/warmup/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod solve_me_first;
pub mod simple_array_sum;
31 changes: 31 additions & 0 deletions tests/hackerrank/warmup/simple_array_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use exercises::hackerrank::warmup::simple_array_sum::simple_array_sum;
use once_cell::sync::Lazy;
use serde::Deserialize;

use crate::common;
use common::utils::load_json;

#[cfg(test)]
mod tests {
use super::*;

#[derive(Debug, Deserialize)]
struct SimpleArraySumTestCase {
input: Vec<i32>,
expected: i32
}

static TEST_DATA: Lazy<Vec<SimpleArraySumTestCase>> =
Lazy::new(|| load_json("tests/data/hackerrank/warmup/simple_array_sum.testcases.json"));

#[test]
fn test_simple_array_sum() {
println!("Testing hackerrank::warmup::simple_array_sum::simple_array_sum()");

for test_case in TEST_DATA.iter() {
let slice: &[i32] = &test_case.input;
let result = simple_array_sum(slice);
assert_eq!(result, test_case.expected);
}
}
}
Loading