Advertisement
Guest User

Untitled

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