From e8edb49b721fecb29546ac73030be2ab17064233 Mon Sep 17 00:00:00 2001 From: Jim Infield Date: Mon, 2 Aug 2021 09:51:06 -0500 Subject: [PATCH] initial codewars worksheet --- src/main.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e7a11a9..c0d3f35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,30 @@ fn main() { - println!("Hello, world!"); + println!(""); + + let strs = vec!["is2 Thi1s T4est 3a"]; + + for str in strs { + println!("{} -> {}", str, order(str)); + } + + println!(""); +} + +fn order(sentence: &str) -> String { + + let mut words = vec![]; + + for word in sentence.split_whitespace() { + let n = word.matches(char::is_numeric).collect::>()[0]; + print!("{:?}\t{:?}\n", n, word); + } + println!(""); + + words = sentence.split_ascii_whitespace() + .map(|s| format!("{}{}", s.matches(char::is_numeric).collect::>()[0],s)) + .collect(); + words.sort(); + + words.iter().map(|w| w.trim_start_matches(char::is_numeric)).collect::>().join(" ") + }