Arrange phrase based on numbers inside each word
xxxxxxxxxx
pub fn arrange_phrase(phrase: &str) -> String {
// split phrase by whitespace
let split = phrase.split_whitespace();
// create a "pairs" data structure which is a vector of Strings.
let mut pairs: Vec<(i32, String)> = vec![];
// loop over split words to build the pairs list.
for word in split {
// loop over the word and extract the numeric string, using it as a "key" to the tupele in the tuple list.
for ch in word.chars() {
if ch.is_numeric() {
let num = ch.to_string().parse::<i32>().unwrap();
let new_word = word.replace(ch, "");
pairs.push((num, new_word));
break;
}
}
}
pairs.sort();
let mut result: String = String::new();
for tup in pairs {
result.push_str(&tup.1);
result.push(' ')
}
result = result.trim().to_string();
result
}