Last active
August 29, 2015 14:23
-
-
Save Ericson2314/f662647a7d6510c0c6d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs::File; | |
use std::path::Path; | |
use std::io::Error; | |
use std::io::BufReader; | |
use std::io::BufRead; | |
use std::collections::HashMap; | |
use std::io::stdin; | |
fn sort_str(s: &str) -> String { | |
let mut v: Vec<char> = s.chars().collect(); | |
v.sort(); | |
v.into_iter().collect() | |
} | |
fn main() { | |
main_err().err().map(|e| panic!("{}", e)); | |
} | |
fn main_err() -> std::io::Result<()> { | |
let path = Path::new("../word.lst"); | |
let file = try!(File::open(&path).map_err(|why| Error::new(why.kind(), | |
format!("failed to open {}: {}", path.display(), why)))); | |
let mut dict: HashMap<String, Vec<String>> = HashMap::new(); | |
for line in BufReader::new(file).lines() { | |
let s = try!(line); | |
dict.entry(sort_str(&s)).or_insert(Vec::new()).push(s); | |
} | |
let sin = stdin(); | |
for line in sin.lock().lines() { | |
let s = try!(line); | |
match dict.get(&sort_str(&s)) { | |
Some(v) => { | |
print!("anagrams for {}:", s); | |
for a in v { | |
print!(" {}", a); | |
} | |
println!(""); | |
}, | |
None => println!("no dice"), | |
} | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment