Created
June 16, 2024 04:07
-
-
Save anataliocs/479bc94ce1ddcb963546a8bae58b4fa6 to your computer and use it in GitHub Desktop.
Rust minmax function with array manipulation
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
fn miniMaxSum(arr: &[i32]) { | |
let len = arr.len(); | |
let mut list: Vec<i64> = vec![]; | |
for n in 0..len { | |
let &sum = &arr.iter() | |
.enumerate() | |
.filter(|(index, &y)| !index.eq(&n)) | |
.map(|(i, &y)| i64::from(y)) | |
.sum::<i64>(); | |
list.push(sum); | |
} | |
let min = list.iter() | |
.min_by(|x, y| x.cmp(y)) | |
.unwrap(); | |
let max = list.iter() | |
.max_by(|x, y| x.cmp(y)) | |
.unwrap(); | |
println!("{min} {max}"); | |
} | |
fn main() { | |
let stdin = io::stdin(); | |
let mut stdin_iterator = stdin.lock().lines(); | |
let arr: Vec<i32> = stdin_iterator.next().unwrap().unwrap() | |
.trim_end() | |
.split(' ') | |
.map(|s| s.to_string().parse::<i32>().unwrap()) | |
.collect(); | |
miniMaxSum(&arr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://docs.rs/appendlist/latest/appendlist/