Advertisement
Guest User

Untitled

a guest
Jul 30th, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. def frequent_words(text)
  2. text.downcase!
  3. words = text.scan(/[[:alpha:]']+/)
  4. frequency = words.tally
  5.  
  6. if frequency.length >= 3
  7. sorted = frequency.sort_by { |key, value| -value }
  8. sorted.each_with_index do |element, index|
  9. puts "#{index + 1} most frequent word is: #{element}" if index < 3
  10. end
  11. else
  12. words.clear
  13. puts "Not enough words!\n#{words}"
  14. end
  15. end
  16.  
  17. frequent_words "hi, there, D'ark brave silly d'Ark, HI, hI) brAvE, d'ARK -hi"
  18.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement