-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: lovesh <[email protected]>
- Loading branch information
Showing
10 changed files
with
150 additions
and
84 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
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
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
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
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 |
---|---|---|
|
@@ -11,3 +11,4 @@ pub mod bbs; | |
pub mod serialization; | ||
pub mod kvac; | ||
pub mod ot; | ||
pub mod statistics; |
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,32 @@ | ||
use std::{ | ||
fmt::Debug, | ||
iter::Sum, | ||
ops::{Add, Div}, | ||
}; | ||
|
||
/// Prints the total, least, median, and the highest value of the given list | ||
pub fn statistics<T, U>(mut values: Vec<T>) -> String | ||
where | ||
T: Copy + Ord + Add<Output = T> + Sum<T> + Div<U, Output = T> + Debug, | ||
U: From<u8>, | ||
{ | ||
values.sort(); | ||
let two = U::from(2); | ||
|
||
let median = { | ||
let mid = values.len() / 2; | ||
if values.len() % 2 == 0 { | ||
(values[mid - 1] + values[mid]) / two | ||
} else { | ||
values[mid] | ||
} | ||
}; | ||
let total: T = values.iter().copied().sum(); | ||
format!( | ||
"{:.2?} | [{:.2?}, {:.2?}, {:.2?}]", | ||
total, | ||
values.first().unwrap(), | ||
median, | ||
values.last().unwrap() | ||
) | ||
} |