Sorting the Hash


#1

puts "whatever"
text = gets.chomp
words = text.split(" ")
puts "whenever"
frequencies = Hash.new[0]
words.each { |word| frequencies[word] += 1}
frequencies = frequencies.sortby{|k,v| v}
frequencies.reverse!

can someone please tell me what i am doin wrong it tells me "Oops, try again. It looks like you haven't set the frequencies hash correctly. Check the first exercise if you need help! "


#2

Sir,

I believe the problem is caused by syntax.

Currently you have the following:

frequencies = Hash.new[0]

Where instead it should be

frequencies = Hash.new(0)

Just replace the "[]" with "()" parenthesis.

Please let me know if this works


#3

And problem is in the:

frequencies = frequencies.sortby{|k,v| v}

instead it should be:

frequencies = frequencies.sort_by{|k,v| v}

#4

puts "your text please"
text = gets.chomp
words = text.split
frequencies = Hash.new (0)
words.each do |words|
frequencies[words] += 1
frequencies = frequencies.sort_by |words, count|
count
frequencies.reverse!
end

I have these changes and am having the same results with the previous question..."Oops, try again.
It looks like you haven't set the frequencies hash correctly. Check the first exercise if you need help!"

Anyone know hwy please let me know.


#5

puts "your text please"
text = gets.chomp
words = text.split
frequencies = Hash.new (0)
words.each do |words|
frequencies[words] += 1
frequencies = frequencies.sort_by {|words, count| count}
frequencies.reverse!
end

I changed it to this and now i get Cant convert into integer?????


#6

For those who wants to go through, here's the winning code lines;

text = gets.chomp
puts text

words = text.split("")

frequencies = Hash.new(0)
words.each do |words| frequencies[words] += 1
puts frequencies[words]
frequencies[words] = frequencies.sort_by {words, count}
end
frequencies[words].reverse!

I end up with 2 syntax errors (8 & 10) but they tell me that Im good to go somehow...


#7

you are missing the sort_by


#8

frequencies[words]=frequencies.sort_by {words, count}

How did i missed the ".sort_by" part?


#9

text = gets.chomp
puts text

words = text.split("")

frequencies = Hash.new(0)
words.each do |words| frequencies[words] += 1
puts frequencies[words]
end
frequencies[words] = frequencies.sort_by do |words, count|
count
end
frequencies[words].reverse!

Update on the exercise, with these lines, the code comes right in the iterator BUT they says I've failed the exercise!
Some help would be required here...


#10

Hey man, have you managed to figure out exactly what was causing the errors?


#11

Nan.
I've decided to kept going and hopefully, an admin/moderator might read these lines.


#12

@mataccord

words = text.split("")

That splits the input into characters instead of words. Do this instead.

words = text.split(" ")


frequencies[words] = ...

You're setting the element at words. You need to be setting the entire hash, like this:

frequencies = ...


I may have missed other errors in your code, but this is what works:

puts "Enter text"
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by do |word, count|
    count
end
frequencies.reverse!

#14