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 $. diff --git a/src/hackerrank/warmup/mod.rs b/src/hackerrank/warmup/mod.rs index 9b41a28..3ada41f 100644 --- a/src/hackerrank/warmup/mod.rs +++ b/src/hackerrank/warmup/mod.rs @@ -1 +1,2 @@ pub mod solve_me_first; +pub mod simple_array_sum; diff --git a/src/hackerrank/warmup/simple_array_sum.rs b/src/hackerrank/warmup/simple_array_sum.rs new file mode 100644 index 0000000..9c587b1 --- /dev/null +++ b/src/hackerrank/warmup/simple_array_sum.rs @@ -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() +} + diff --git a/tests/data/hackerrank/warmup/simple_array_sum.testcases.json b/tests/data/hackerrank/warmup/simple_array_sum.testcases.json new file mode 100644 index 0000000..c916b56 --- /dev/null +++ b/tests/data/hackerrank/warmup/simple_array_sum.testcases.json @@ -0,0 +1,6 @@ +[ + { + "input": [1, 2, 3, 4, 10, 11], + "expected": 31 + } +] diff --git a/tests/hackerrank/warmup/mod.rs b/tests/hackerrank/warmup/mod.rs index 9b41a28..3ada41f 100644 --- a/tests/hackerrank/warmup/mod.rs +++ b/tests/hackerrank/warmup/mod.rs @@ -1 +1,2 @@ pub mod solve_me_first; +pub mod simple_array_sum; diff --git a/tests/hackerrank/warmup/simple_array_sum.rs b/tests/hackerrank/warmup/simple_array_sum.rs new file mode 100644 index 0000000..0cf9ed8 --- /dev/null +++ b/tests/hackerrank/warmup/simple_array_sum.rs @@ -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, + expected: i32 + } + + static TEST_DATA: Lazy> = + 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); + } + } +}