Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.53 KB | None | 0 0
  1. def statystyka_slow(n)
  2.     hash = Hash.new
  3.     File.open(n, "r").each_line { |line|
  4.     words = line.downcase.gsub(/[^[:word:]\s]/, '').split
  5.     words.each { |word|
  6.         if hash.has_key?(word)
  7.             hash[word] = hash[word] + 1
  8.         else
  9.             hash[word] = 1
  10.         end
  11.         }
  12.     }
  13.  
  14.     sum = hash.values.inject(:+)
  15.  
  16.     hash.each{ |key,value| hash[key] = (value.to_f*100/sum).to_f.round(4)}
  17.     hash.each { |elem|
  18.       puts "\"#{elem[0]}\" = #{elem[1]}%"
  19.     }
  20.    
  21. end
  22.  
  23. statystyka_slow(gets.chop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement