Skip to content

Instantly share code, notes, and snippets.

@Crispy13
Last active June 8, 2024 11:55
Show Gist options
  • Save Crispy13/b254fc740a48823414c3afb8264254d7 to your computer and use it in GitHub Desktop.
Save Crispy13/b254fc740a48823414c3afb8264254d7 to your computer and use it in GitHub Desktop.
make sorted string
use std::fmt::Write;
use polars::prelude::*;
fn make_sorted_array_series(input: Vec<String>, datatype: &DataType) -> Vec<String> {
input
.into_iter()
.map(|s| {
let splited = s.split(",");
match datatype {
DataType::Int32 => {
let mut item_vec = splited
.map(|e| e.parse::<i32>().unwrap())
.collect::<Vec<_>>();
item_vec.sort();
let mut new_s = item_vec.into_iter().fold(String::with_capacity(s.len()), |mut a, b| {
write!(&mut a, "{},", b).unwrap();
a
});
new_s.pop().unwrap();
new_s
}
_ => todo!(),
}
})
.collect::<Vec<_>>()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sorted_element_of_vec() {
let a = [
"123,456,789,1,-1,0,22,-345",
"123,456",
"123,0,22,-345",
"123,456,789,-345",
].into_iter().map(ToString::to_string).collect::<Vec<_>>();
let answer = [
"-345,-1,0,1,22,123,456,789",
"123,456",
"-345,0,22,123",
"-345,123,456,789",
].into_iter().map(ToString::to_string).collect::<Vec<_>>();
assert_eq!(answer,make_sorted_array_series(a, &DataType::Int32));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment