5/8 Irterating over the array


#1

hey guys i need help.
I cant seem to find the mistake that won't let my code get accepted.
I've been looking at the introduction 1/8 and found that my code is the same as the one there.
Here's my code
puts "Text"
user_input = gets.chomp
words = text.split (" ")
frequencies = Hash.new(0)

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

I'd really apperciate it if someone could go through my code and point out the mistake for me.
thanks a lot


#2

Where did you declare the object text? And when did you assign it a string?


#3

What do you mean by this @arjofocolovi ?


#4

bolu001
In your code you used "user_input" as a variable and later on you typed "text" in "text.split(" ")". Therefore, it should be either "user_input" or "text" in both lines like this:

puts "Text"
user_input = gets.chomp
words = user_input.split (" ")

OR

puts "Text"
text = gets.chomp
words = text.split (" ")

:wink:


#5

So would this be right?

puts "Text"
user_input = gets.chomp
words = user_input.split(" ")

words = user_input.split(" ")
frequencies=Hash.new(0)

words.each = {|word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {|word, count| count}


#6

is @zvit3k there? I still need the help?!!


#7

Hey,

@bolu001
First of all, you typed "words = user_input.split(" ")" 2 times. There is no such need :smile:

Secondly, in this line "words.each = {|word| frequencies[word] += 1 }" erase equation mark , because you are not signing the value - you use method.

The last thing to do is adding ""frequencies.reverse!", at the end of the program.

I think this should run the program completely :smile:


#8

So what about this:

puts "Text"
user_input = gets.chomp

words = user_input.split(" ")
frequencies=Hash.new(0)

words = {"Hello" => 4, "Codecademy" => 9}
words["Hello"] += 1
puts words["Hello"]

words.each {|word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {|word, count| count}
frequencies.reverse!


#9

Please help me with my code...I am lost in "Translation"

Here is my code:

puts "Text please: "
text = gets.chomp

words = text.split(" ")
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {|a, b| b }
frequencies.reverse!
frequencies.each { |word, frequency| puts word + " " + frequency.to_s }

Here is what it tells me after I try to save & submit

"It looks like you haven't set the frequencies hash correctly. Check the Hint if you need help!"

I still can't figure it out even after looking at the Hint! Any suggestions/advice would be greatly appreciated! Thankyou in advance!!!!


#10

puts "Put some text here"
words = gets.chomp.split (" ")
frequencies = Hash.new(0)
words.each do
|words| frequencies[words] += 1
end
frequencies.each do
| words, frequencies|
print words + " " + frequencies.to_s + " "
end

Alternatively the ending could look like

frequencies.each { | words, frequencies| print words + " " + frequencies.to_s + " " }

Explained
Line 1 = Asks for user input
Line 2 = Creates the variable 'words' which is equal to the user input of 'puts' > .split makes it an array
Line 3 = Creates a hash called frequencies. The 0 means the default value of a key is 0. Thus where the hash has no value the key has a default of 0.
Line 4 = words.each = for every word "do" the following
Line 5 = For each frequency we are running the key words against the value of frequencies words.each = for every word "do" the following. =+ 1 goes to the next array item
Line 6 = Every do must have an end.
Alternatively to using Do and End you could chose to use. frequencies.each { Code }. This has the same effect as do and end. It's simply a different way of writing it.
Line 7 = A command .each to do something to each key of frequencies
Line 8 = For each frequency we are running the key words against the value of frequencies
Line 9 = Simply prints the line to the console. Note ".to_s" means "Convert integer to string". " " is simply to add a space
Line 10 = As line 6


#11

Wow, thank you! Really appreciate your explanation per line!


#12

Here's the string of code that I used, and it worked without the curly brackets.. not sure why!

puts "say something"
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!


#13

I had the same problem, and mine worked when I use "do/end" instead of the brackets. I found it a bit confusing choosing which variables to use at each place.


#14

Very helpful, thank you :slightly_smiling:


#15

What do you mean equation mark which one ???? @zvit3k