Odd words: Difference between revisions
Content added Content deleted
(Added C++ solution) |
(Added Rust solution) |
||
Line 691: | Line 691: | ||
13. supervene >> spree |
13. supervene >> spree |
||
14. terminable >> trial |
14. terminable >> trial |
||
</pre> |
|||
=={{header|Rust}}== |
|||
<lang rust>use std::collections::BTreeSet; |
|||
use std::fs::File; |
|||
use std::io::{self, BufRead}; |
|||
fn load_dictionary(filename: &str, min_length: usize) -> std::io::Result<BTreeSet<String>> { |
|||
let file = File::open(filename)?; |
|||
let mut dict = BTreeSet::new(); |
|||
for line in io::BufReader::new(file).lines() { |
|||
let word = line?; |
|||
if word.len() >= min_length { |
|||
dict.insert(word); |
|||
} |
|||
} |
|||
Ok(dict) |
|||
} |
|||
fn print_words(words: &[(&String, String)]) { |
|||
for (i, (a, b)) in words.iter().enumerate() { |
|||
println!("{:2}: {:<14}{}", i + 1, a, b); |
|||
} |
|||
} |
|||
fn main() { |
|||
let min_length = 5; |
|||
match load_dictionary("unixdict.txt", min_length) { |
|||
Ok(dictionary) => { |
|||
let mut odd_words = Vec::new(); |
|||
let mut even_words = Vec::new(); |
|||
for word in &dictionary { |
|||
if word.len() < min_length + 2 * (min_length / 2) { |
|||
continue; |
|||
} |
|||
let mut odd_word = String::new(); |
|||
let mut even_word = String::new(); |
|||
for (i, c) in word.chars().enumerate() { |
|||
if (i & 1) == 0 { |
|||
odd_word.push(c); |
|||
} else { |
|||
even_word.push(c); |
|||
} |
|||
} |
|||
if dictionary.contains(&odd_word) { |
|||
odd_words.push((word, odd_word)); |
|||
} |
|||
if dictionary.contains(&even_word) { |
|||
even_words.push((word, even_word)); |
|||
} |
|||
} |
|||
println!("Odd words:"); |
|||
print_words(&odd_words); |
|||
println!("\nEven words:"); |
|||
print_words(&even_words); |
|||
} |
|||
Err(error) => eprintln!("{}", error), |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Odd words: |
|||
1: barbarian brain |
|||
2: childbear cider |
|||
3: corrigenda cried |
|||
4: gargantuan grata |
|||
5: headdress hades |
|||
6: palladian plain |
|||
7: propionate point |
|||
8: salvation slain |
|||
9: siltation slain |
|||
10: slingshot sight |
|||
11: statuette saute |
|||
12: supersede spree |
|||
13: supervene spree |
|||
14: terminable trial |
|||
Even words: |
|||
1: cannonball annal |
|||
2: importation motto |
|||
3: psychopomp scoop |
|||
4: starvation train |
|||
5: upholstery posey |
|||
</pre> |
</pre> |
||