Can somebody help me on this one and try to explain this to me?


Here is my code :slight_smile:

puts " Input a Number: "
text = gets.chomp

words = text.split (" ")

frequencies = (0)

words.each { |x| frequencies[x] += 1 }

frequencies = frequencies.sort_by { | 0 , 100 | 100}



Use .sort_by to sort the frequencies hash by word count, like step 2 above. Store the result back in frequencies.
Use .reverse! to reverse the sorted frequencies array.
Check the Hint for more details!

Our first step should be to reassign frequencies to its sorted version (.sort_by doesn't sort the hash in-place—it will create a copy that is sorted):

frequencies = frequencies.sort_by { |k, v| v }
This actually returns an array of values, which you can then reverse in-place with reverse!:



What does this line mean:

frequencies = frequencies.sort_by { | 0 , 100 | 100}


You have the correct code in the hint, so I don't really know why you have used 0 and 100. Please explain why you modified this line and I will explain what's wrong.


this has been solved I understand now instead of using numbers as your variable use a string instead


ok i have an problem here:

frequencies = frequencies.sort_by {|a, b| b }

what does this line do exactly?
i am pretty sure that "a" stands for the words and "b" for the number connected to the word but what does the second "b" do?


This line says sort frequencies by b (second b) where a is a key and b is a value (|a, b|). So, in other words, it simply sorts frequencies by the values.


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