pub fn pancake_sort(list: &[i32]) -> Result<Vec<i32>,String> {
if list.is_empty() {
return Err("Cannot use pancake sort on an empty list!".to_string());
}
if list.len() == 1 {
return Ok(list.to_vec());
}
let mut result = list.to_vec();
for i in (2..=result.len()).rev() {
let index_max = result[0..i].iter().enumerate().max_by_key(|&(_idx, &val)| val).map(|(idx, _val)| idx).unwrap_or(0);
if index_max != i - 1 {
if index_max != 0 {
result[0..=index_max].reverse();
}
result[0..i].reverse();
}
}
Ok(result)
}