use std::io; // bring flush() into scope with `use std::io::Write`
use std::io::prelude::*; // Bring `std::io::BufRead` in scope
+use statrs::distribution::{Exp, ContinuousCDF}; // Continuous needed for pdf
+// use statrs::statistics::Distribution;
fn input() -> Vec<String> {
// Consider datastructure some list of strings
v
}
-fn output(prio: Vec<f64>, ranked: Vec<String>) {
+fn output(prio: &Vec<f64>, ranked: &Vec<String>) {
for (p, l) in prio.iter().zip(ranked.iter()) {
println!("{:2.0} %\t{}", p * 100.0, l);
}
}
+fn normalize(mut arg: Vec<f64>) -> Vec<f64> {
+ let mut sum = 0.0;
+ for v in &arg {
+ sum += v;
+ }
+ for i in 0..arg.len() {
+ arg[i] /= sum;
+ }
+ arg
+}
+
+
fn delta(n: i32) -> Vec<f64> {
- let mut ret: Vec<f64> = Vec::new();
- for _i in 0..n {
- ret.push(1.0 / n as f64);
+ let mut prio: Vec<f64> = Vec::new();
+ const MEAN: f64 = 1.0; // unessential thanks to normalization
+ for i in 1..n + 1 {
+ prio.push(MEAN);
}
- ret
+ prio.reverse();
+ normalize(prio)
}
+fn exp(n: i32) -> Vec<f64> {
+ const RATE: f64 = 1.0; // rate is unessential thanks to normalization
+ let mut prio: Vec<f64> = Vec::new();
+ let dist = Exp::new(RATE).unwrap();
+ for i in 1..n + 1 {
+ let f = i as f64 / (n as f64 + 1.0);
+ let x = dist.inverse_cdf(f);
+ prio.push(x);
+ }
+ prio.reverse();
+ normalize(prio)
+}
+
+
fn main() {
+
let ranked: Vec<String> = input();
let num = ranked.len() as i32;
- let prio = delta(num);
- for p in prio.iter() {
- eprint!("{} ", p);
- }
- eprintln!("");
- for l in ranked.iter() {
- eprint!("{} ", l);
+ let _prio1 = delta(num);
+ let prio2 = exp(num);
+
}
eprintln!("");
eprintln!("That was {} items: {}", num, prio[0]);
- output(prio, ranked);
+ output(&prio2, &ranked);
}