Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. use std::collections::BTreeMap;
  2. use std::io;
  3. use std::io::BufRead;
  4.  
  5. fn main() {
  6. let mut counts: BTreeMap<String, isize> = BTreeMap::new();
  7. let stdin = io::stdin();
  8. for line_result in stdin.lock().lines() {
  9. match line_result {
  10. Ok(line) => {
  11. let lowercase_line = line.to_lowercase();
  12. let words = lowercase_line.split(|c: char| {
  13. !(c.is_alphabetic() || c == ''')
  14. }).filter(|s| !s.is_empty());
  15. for word in words {
  16. *(counts.entry(word.to_string()).or_insert(0)) += 1;
  17. }
  18. },
  19. Err(e) => {
  20. panic!("Error parsing stdin: {:?}", e);
  21. }
  22. }
  23. }
  24. for (key, value) in counts.iter() {
  25. println!("{} {}", key, value);
  26. }
  27. }
  28.  
  29. counts = Dict{AbstractString, UInt64}()
  30. for line in eachline(STDIN)
  31. for word in matchall(r"[a-z']+", lowercase(line))
  32. counts[word] = get(counts, word, 0) + 1
  33. end
  34. end
  35. for (word, count) in sort(collect(counts))
  36. println("$word $count")
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement