#sample solution
puts "Enter a phrase: "
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new(0)
words.each {|element| frequencies[element] += 1}
frequencies = frequencies.sort_by {|a,b| b}
frequencies = frequencies.reverse!
frequencies.each {|word, frequency| puts "#{word} #{frequency.to_s}"}
No comments:
Post a Comment