Skip to content

Instantly share code, notes, and snippets.

@lilyball
Created February 26, 2014 20:35
Show Gist options
  • Save lilyball/9237968 to your computer and use it in GitHub Desktop.
Save lilyball/9237968 to your computer and use it in GitHub Desktop.
extern crate collections;
use std::str;
use collections::HashMap;
use std::io::File;
use std::io::BufferedReader;
use std::cmp;
fn sorted_key(string: &str) -> ~[u8] {
// if we need to handle decomposed vs composed then we should use
// string.nfd_chars() or string.nfkd_chars(). The latter will also handle
// compatibility decomposition.
// But I think we can safely assume ascii for this, so don't bother
let mut bytes = string.as_bytes().to_owned();
bytes.sort();
bytes
}
fn main () {
let path = Path::new("unixdict.txt");
let mut file = BufferedReader::new(File::open(&path));
let mut map: HashMap<~[u8], ~[~str]> = HashMap::new();
for line in file.lines() {
let s = line.trim().to_owned();
map.mangle(sorted_key(s), s,
|_k, s| ~[s],
|_k, v, s| v.push(s));
}
let max_length = map.values().max_by(|v| v.len()).unwrap().len();
for v in map.values().filter(|v| v.len() == max_length) {
for s in v.iter() {
print!("{} ", *s)
}
println!("")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment