Skip to content

Instantly share code, notes, and snippets.

@Denommus
Last active August 29, 2015 14:02
Show Gist options
  • Save Denommus/93e1f4da07240e5aa949 to your computer and use it in GitHub Desktop.
Save Denommus/93e1f4da07240e5aa949 to your computer and use it in GitHub Desktop.
Showing off for Python programmers
import Control.Applicative
import Data.List.Split
import Data.List
main :: IO ()
main = do
content <- lines <$> readFile "massa.txt"
let lasts = last . splitOn "." <$> content
let counting = (\xs@(x:_) -> (length xs, x)) . group . sort <$> lasts
let output = sortBy (flip compare) counting
mapM_ (putStrLn . \(x, y) -> show x ++ " " ++ y) output
use std::io::{File, BufferedReader};
use std::collections::HashMap;
pub fn main() {
let mut content = BufferedReader::new(File::open(&Path::new("massa.txt")));
let content_lines = content.lines();
let lasts = content_lines.map(|x| x.unwrap().as_slice().split('.')
.last().unwrap().to_string());
let mut output: Vec<_> = lasts.fold(HashMap::<String, uint>::new(), |mut h, l| {
if h.contains_key(&l) {
*h.get_mut(&l).unwrap() += 1;
} else {
h.insert(l, 1);
}
h
}).into_iter().collect();
output.sort_by(|&(_, ref x), &(_, y)| y.cmp(x));
for &(ref x, y) in output.iter() {
print!("{} {}", y, x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment