Quotes help make search much faster. Example: "Practice Makes Perfect"

Tuesday, November 20, 2012

Codecademy: "Iterating Over the Hash"


#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


This is an example of scrolling text using Javascript.

Popular Posts