Skip to content

Commit ce5faed

Browse files
authored
Merge pull request #27 from sir-gon/feature/plus_minus
[Hacker Rank]: Plus Minus solved ✓
2 parents af68a4a + 5404bb9 commit ce5faed

File tree

6 files changed

+159
-0
lines changed

6 files changed

+159
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# [Plus Minus](https://www.hackerrank.com/challenges/plus-minus)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Given an array of integers, calculate the ratios of its elements
7+
that are positive, negative, and zero. Print the decimal value of
8+
each fraction on a new line with 6 places after the decimal.
9+
10+
**Note**: This challenge introduces precision problems.
11+
The test cases are scaled to six decimal places, though answers
12+
with absolute error of up to $ 10^{-4} $ are acceptable.
13+
14+
## Example
15+
16+
$ arr = [1, 1, 0, -1, -1] $
17+
18+
There are $ n = 5 $ elements, two positive, two negative and one zero.
19+
Their ratios are $ 2/5 = 0.400000 $, $ 2/5 = 0.400000 $ and $ 1/5 = 0.200000 $.
20+
Results are printed as:
21+
22+
```text
23+
0.400000
24+
0.400000
25+
0.200000
26+
```
27+
28+
## Function Description
29+
30+
Complete the plusMinus function in the editor below.
31+
plusMinus has the following parameter(s):
32+
33+
- int arr[n]: an array of integers
34+
35+
## Print
36+
37+
Print the ratios of positive, negative and zero values in the array.
38+
Each value should be printed on a separate line with $ 6 $ digits after
39+
the decimal. The function should not return a value.
40+
41+
## Input Format
42+
43+
The first line contains an integer, `n`, the size of the array.
44+
The second line contains `n` space-separated integers that describe `arr[n]`.
45+
46+
## Constraints
47+
48+
$ 0 < n \leq 100 $ \
49+
$ -100 \leq arr[i] \leq 100 $
50+
51+
## Output Format
52+
53+
**Print** the following lines, each to decimals:
54+
55+
1. proportion of positive values
56+
2. proportion of negative values
57+
3. proportion of zeros
58+
59+
## Sample Input
60+
61+
```text
62+
STDIN Function
63+
----- --------
64+
6 arr[] size n = 6
65+
-4 3 -9 0 4 1 arr = [-4, 3, -9, 0, 4, 1]
66+
```
67+
68+
## Sample Output
69+
70+
```text
71+
0.500000
72+
0.333333
73+
0.166667
74+
```
75+
76+
## Explanation
77+
78+
There are $ 3 $ positive numbers, negative numbers, and $ 1 $ zero in the array.
79+
The proportions of occurrence are positive: $ 3/6 = 0.500000 $,
80+
negative: $ 2/6 = 0.333333 $ and zeros: $ 1/6 = 0.166667 $.

src/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pub mod simple_array_sum;
33
pub mod compare_triplets;
44
pub mod a_very_big_sum;
55
pub mod diagonal_difference;
6+
pub mod plus_minus;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/solveMeFirst.md]]
2+
3+
pub fn plus_minus_calculate(arr: &[i32]) -> (f64, f64, f64) {
4+
let n = arr.len() as f64;
5+
let (mut pos, mut neg, mut zero) = (0, 0, 0);
6+
7+
for &num in arr {
8+
if num > 0 {
9+
pos += 1;
10+
} else if num < 0 {
11+
neg += 1;
12+
} else {
13+
zero += 1;
14+
}
15+
}
16+
17+
(pos as f64 / n, neg as f64 / n, zero as f64 / n)
18+
}
19+
20+
pub fn plus_minus_calculate_string(arr: &[i32]) -> (String, String, String) {
21+
let (pos, neg, zero) = plus_minus_calculate(arr);
22+
(format!("{:.6}", pos, ), format!("{:.6}", neg), format!("{:.6}", zero))
23+
}
24+
25+
pub fn plus_minus(arr: &[i32]) {
26+
let (pos, neg, zero) = plus_minus_calculate_string(arr);
27+
28+
println!("{}", pos);
29+
println!("{}", neg);
30+
println!("{}", zero);
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"title": "test case 0",
4+
"input": [-4, 3, -9, 0, 4, 1],
5+
"expected": ["0.500000", "0.333333", "0.166667"]
6+
}
7+
]

tests/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pub mod simple_array_sum;
33
pub mod compare_triplets;
44
pub mod a_very_big_sum;
55
pub mod diagonal_difference;
6+
pub mod plus_minus;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use serde::Deserialize;
2+
use once_cell::sync::Lazy;
3+
4+
use exercises::hackerrank::warmup::plus_minus::plus_minus;
5+
use exercises::hackerrank::warmup::plus_minus::plus_minus_calculate_string;
6+
use crate::common::utils::load_json;
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use super::*;
11+
12+
#[derive(Debug, Deserialize)]
13+
struct PlusMinusTestCase {
14+
title: String,
15+
input: Vec<i32>,
16+
expected: Vec<String>,
17+
}
18+
19+
static TEST_DATA: Lazy<Vec<PlusMinusTestCase>> =
20+
Lazy::new(|| load_json("tests/data/hackerrank/warmup/plus_minus.testcases.json"));
21+
22+
#[test]
23+
fn test_plus_minus() {
24+
for test_case in TEST_DATA.iter() {
25+
println!("Testing hackerrank::warmup::plus_minus::plus_minus() : {:?}", test_case.title);
26+
27+
let expected: (_, _, _) = (
28+
test_case.expected[0].clone(),
29+
test_case.expected[1].clone(),
30+
test_case.expected[2].clone()
31+
);
32+
let result = plus_minus_calculate_string(test_case.input.as_slice());
33+
assert_eq!(result, expected);
34+
35+
// call printer for coverage
36+
plus_minus(test_case.input.as_slice());
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)