Last active
June 5, 2021 15:22
-
-
Save claudioacioli/9e9939ea531d4f3792c5622465caebe1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
fn main() { | |
let (l, arr) = get_input(); | |
let (p, n, z) = plus_minus(&arr); | |
println!( | |
"{:.6}\n{:.6}\n{:.6}", | |
avg(p as f32, l as f32), | |
avg(n as f32, l as f32), | |
avg(z as f32, l as f32) | |
); | |
} | |
fn get_input () -> (usize, Vec<i32>) { | |
let mut s = String::new(); | |
io::stdin() | |
.read_line(&mut s) | |
.expect("Failed to read line"); | |
let n = s.trim() | |
.parse::<usize>() | |
.expect("Invalid input"); | |
s.clear(); | |
io::stdin() | |
.read_line(&mut s) | |
.expect("Failed to read line"); | |
let mut arr = vec![0; n]; | |
for (i, value) in s.trim().split_whitespace().enumerate() { | |
if i > n { | |
break; | |
} | |
arr[i] = value.parse::<i32>().unwrap_or(0); | |
} | |
(n, arr) | |
} | |
fn avg(x :f32, y :f32) -> f32 { | |
if y == 0.0 { | |
0.0 | |
} else { | |
x / y | |
} | |
} | |
fn plus_minus(arr: &[i32]) -> (i32, i32, i32) { | |
let mut p = 0; | |
let mut n = 0; | |
let mut z = 0; | |
for i in arr.iter() { | |
if *i > 0 { | |
p += 1; | |
} else if *i < 0 { | |
n += 1; | |
} else { | |
z += 1; | |
} | |
} | |
(p, n, z) | |
} | |
#[test] | |
fn test_plus_minus() { | |
let (x, y, z) = plus_minus(&[1,1,1,0,-1,-1]); | |
assert_eq!(3, x); | |
assert_eq!(2, y); | |
assert_eq!(1, z); | |
} | |
#[test] | |
fn test_avg() { | |
assert_eq!(2.0, avg(10.0, 5.0)); | |
assert_eq!(3.0, avg(9.0, 3.0)); | |
assert_eq!(4.0, avg(16.0, 4.0)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment