RosettaCodeData/Task/Ordered-words/Rust/ordered-words.rust

44 lines
924 B
Text
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
const FILE: &'static str = include_str!("./unixdict.txt");
2013-04-10 22:43:41 -07:00
fn is_ordered(s: &str) -> bool {
let mut prev = '\x00';
2016-12-05 22:15:40 +01:00
for c in s.to_lowercase().chars() {
2013-10-27 22:24:23 +00:00
if c < prev {
2013-04-10 22:43:41 -07:00
return false;
}
prev = c;
}
return true;
}
2016-12-05 22:15:40 +01:00
fn find_longest_ordered_words(dict: Vec<&str>) -> Vec<&str> {
2015-11-18 06:14:39 +00:00
let mut result = Vec::new();
2013-04-10 22:43:41 -07:00
let mut longest_length = 0;
2015-11-18 06:14:39 +00:00
for s in dict.into_iter() {
if is_ordered(&s) {
2013-04-10 22:43:41 -07:00
let n = s.len();
if n > longest_length {
longest_length = n;
2013-10-27 22:24:23 +00:00
result.truncate(0);
2013-04-10 22:43:41 -07:00
}
if n == longest_length {
2016-12-05 22:15:40 +01:00
result.push(s);
2013-04-10 22:43:41 -07:00
}
}
}
return result;
}
fn main() {
2016-12-05 22:15:40 +01:00
let lines = FILE.lines().collect();
2013-04-10 22:43:41 -07:00
2014-01-17 05:32:22 +00:00
let longest_ordered = find_longest_ordered_words(lines);
2013-04-10 22:43:41 -07:00
2013-10-27 22:24:23 +00:00
for s in longest_ordered.iter() {
2015-11-18 06:14:39 +00:00
println!("{}", s.to_string());
2013-04-10 22:43:41 -07:00
}
}