Skip to content

Instantly share code, notes, and snippets.

@MattPorto
Created September 13, 2016 14:04
Show Gist options
  • Save MattPorto/7e62c2e301034610942a95216211a936 to your computer and use it in GitHub Desktop.
Save MattPorto/7e62c2e301034610942a95216211a936 to your computer and use it in GitHub Desktop.
recebe um texto e conta quantas vezes cada palavra apareceu no texto
puts "Digite o texto:"
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new(0)
words.each { |x| frequencies["#{x}"] += 1}
frequencies = frequencies.sort_by do |word, count|
count
end
frequencies.reverse!
frequencies.each { |word, count| puts word + " " + count.to_s}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment