6. Sorting the Hash


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-693PD/0/6?curriculum_id=5059f8619189a5000201fbcb


So, here is my code


puts "Hello, write down anything"
text = gets.chomp

words = text.split

frequencies = Hash.new 0

words.each do |key|
    frequencies[key] += 1
end


frequencies = frequencies.sort_by do |freqencies, count|
    count
end

frequencies.reverse!

For the system everything is okay, but I think something is wrong. Maybe everything is right, but I don't get it, so if you can, please explain to me.

If my input is something like: dog, cat, lion, tiger, wolf. It returns this [["wolf", 1], ["tiger,", 1], ["lion,", 1], ["cat,", 1], ["dog,", 1]]. It only reverses ordering of words, shouldn't it also increment numbers beside? I mean: [["wolf", 1], ["tiger", 2], ["lion", 3]]... and so on?


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.