Skip to content

Commit

Permalink
Added position average solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Nov 25, 2023
1 parent b3d9e60 commit a8af57a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ other languages.
- Spinning Words (https://www.codewars.com/kata/5264d2b162488dc400000001)
- Camel Case (https://www.codewars.com/kata/517abf86da9663f1d2000003)
- Count Characters (https://www.codewars.com/kata/52efefcbcdf57161d4000091)
- Position Average (https://www.codewars.com/kata/59f4a0acbee84576800000af)

#### Kata 7

Expand Down
1 change: 1 addition & 0 deletions src/codewars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ mod people_on_the_bus;
mod summation;
mod not_secure;
mod count_characters;
mod position_average;
27 changes: 27 additions & 0 deletions src/codewars/position_average.rs
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)
}
}

0 comments on commit a8af57a

Please sign in to comment.