Generation

generate functionSat, 12 Nov 2022

Find median of array numbers and return them

fn median(arr: &[f32]) -> f32 { let mut length: f32 = arr.len() as f32; let mut length_i32: i32 = arr.len() as i32; let mut arr_sorted = arr.clone(); arr_sorted.sort_by(|a, b| a.partial_cmp(b).unwrap()); if length % 2. == 0. { (arr_sorted[length_i32/2] + arr_sorted[(length_i32/2)-1])/2. } else { arr_sorted[(length_i32/2) as usize] } }

Questions about programming?Chat with your personal AI assistant