Created
June 5, 2021 19:05
-
-
Save claudioacioli/7e6fca68123bbcc053c587a746df368d to your computer and use it in GitHub Desktop.
Compare the Triplets (https://www.hackerrank.com/challenges/compare-the-triplets/problem)
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 a = get_arr(); | |
let b = get_arr(); | |
let result = compare_rates(&a, &b); | |
println!("{} {}", result.0, result.1); | |
} | |
fn get_arr() -> Vec<u8> { | |
const N :usize = 3; | |
let mut s = String::new(); | |
io::stdin().read_line(&mut s).unwrap_or(0); | |
let mut arr = vec![0; N]; | |
for (i, value) in s.split_whitespace().enumerate() { | |
if i >= N { | |
break; | |
} | |
arr[i] = value.parse::<u8>().unwrap_or(0); | |
} | |
arr | |
} | |
fn compare_rates(a :&[u8], b :&[u8]) -> (u8, u8) { | |
if a.len() != 3 || b.len() != 3 { | |
return (0, 0); | |
} | |
let mut score_a :u8 = 0; | |
let mut score_b :u8 = 0; | |
for i in 0..3 { | |
if a[i] > b[i] { | |
score_a += 1; | |
} else if b[i] > a[i] { | |
score_b += 1; | |
} | |
} | |
(score_a, score_b) | |
} | |
#[test] | |
fn test_compare_rates() { | |
assert_eq!((1, 2), compare_rates(&[95, 50, 75], &[100, 45, 95])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment