kittenpub / database-repository

This repository contains datasets included in various technical professional books published by GitforGits

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Statistics with Rust" book ch. 3 issues

dxps opened this issue · comments

Continuing to capture the issues (and potential fixes, as in this case) as I previously started in #1 , in chapter 3 of the book I got into the following issue(s):

  1. In Calculate Measure of Central Tendency section, sort_unstable() cannot be performed on an array of f64.
    • This is a known behavior.
    • The workaround is to use data.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());

  2. The mode(data: &[f64]) implementation cannot compile since you cannot use an f64 as a key of a HashMap.
    • I used ordered_float crate and this implementation:
      pub fn mode(data: &[f64]) -> Option<f64> {
          let mut frequency_map = HashMap::<OrderedFloat<f64>, i32>::new();
          for &value in data {
              let count = frequency_map.entry(OrderedFloat(value)).or_insert(0);
              *count += 1;
          }
          frequency_map
              .into_iter()
              .max_by_key(|&(_, count)| count)
              .map(|(value, _)| value.0)
      }