-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,4 @@ mod people_on_the_bus; | |
mod summation; | ||
mod not_secure; | ||
mod count_characters; | ||
mod position_average; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
fn position_average(s: &str) -> f64 { | ||
let values = s.split(", ").collect::<Vec<&str>>(); | ||
let mut equal_count = 0; | ||
let mut count = 0; | ||
for (index, value) in values[..values.len() - 1].iter().enumerate() { | ||
for v in values[index + 1..].iter() { | ||
equal_count += value.chars().zip(v.chars()).filter(|(a, b)| a == b).count(); | ||
count += value.len() | ||
} | ||
} | ||
|
||
(equal_count as f64 / count as f64) * 100.0 | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use test_case::test_case; | ||
|
||
use crate::codewars::position_average::position_average; | ||
|
||
// TODO: Additional test cases would be great but cant be bothered to add the float_eq macro atm. | ||
#[test_case("123, 123", 100.0)] | ||
fn test_position_average(input: &str, expected: f64) { | ||
let average = position_average(input); | ||
assert_eq!(average, expected) | ||
} | ||
} |