8/8 Making sure learning is occuring


#1



Hello all,

     I am fairly new to programming and ruby. This lesson has been the toughest one so far for me so I would like to make sure that I have grasped the information given in this lesson. Below I have my code and the comments that go along with what I think is going on. There are a couple of questions that I have after the code. Please feel free to correct me and provide me with your collective wisdom.

 
*************************************************************************************
#puts a string on the screen
puts "please insert some text:"

=begin
collects input (using "gets" command) from the end user (while ".chomp" method deletes the line break) saves it under the variable text
=end
    text = gets.chomp

=begin 
splits the text into an array using a space as the indicator
=end
    words = text.split(" ")

=begin
a new blank hash is created with a default value of 0 and saved under the variable frequencies
=end
frequencies = Hash.new(0)

=begin
we take the array created previously by the splitting of the text through the use of the .split method and for each element we increase the value by 1
=end
words.each {|x| frequencies[x] += 1}

=begin
frequencies is set equal to frequencies.sort_by which takes each key and value and sorts it by the value
=end
frequencies = frequencies.sort_by do |word, count| count

=begin
ends the block of code that needs to be executed for each key => value pair_
=end
end

=begin
takes current values of frequencies and reverses it making the first value the one that is used the most
=end
frequencies.reverse!

=begin
frequencies is iterated to take each word and count and puts it onto the screen for the end user to see, and what they will see is the word followed by a space and the count of how many times it is used then the block ends_
=end
frequencies.each do |word, count|
    puts word + " " + count.to_s
end
*************************************************************************************

my questions

1.      words.each {|x| frequencies[x] += 1}
the iteration to increase the value of the has by 1 there is a [x] after the word frequencies i dont recall learning this in previous lesson and would like to know what it is and how it works.

2. is there any real difference whether you write it as: 
        words.each {|x| frequencies[x] += 1} 
or 
        words.each {|x| x = frequencies[x] += 1}
I wrote it the second way because that is the way the lesson taught it and it still works but I have seen people write it the first way more often

3.      frequencies = frequencies.sort_by do |word, count| count
why do we need to make frequencies equal to frequencies.sort_by could we not just remove the "frequencies =" portion


Frequencies[word] (Have answer correct, but would like to understand it)
#2

Good questions. I've changed the comments in your code. I hope this makes it easier to read.

# split text into an array.
words = text.split(" ")

# Create blank Hash with default value 0.
frequencies = Hash.new(0)

# Increase each value element in words by 1.
words.each {|x| frequencies[x] += 1}

# Sort key, value pairs using frequencies.sort_by. Assign result to frequencies.
frequencies = frequencies.sort_by do |word, count| count

end

# Reverse frequencies so value are descending.
frequencies.reverse!

# Iterate over frequencies using .each. 
# Convert count to string and puts word and count, separated by a space.
frequencies.each do |word, count|
    puts word + " " + count.to_s

end

Question 1:

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

The iteration increases the value by 1. There is [x] after the word frequencies
which I don't recall learning about in a previous lesson. What it is and how it works?

Answer:

Look back to the previous module: Data Structures; Page 6
– Intro to Hashes. A hash is built up from key value pairs using syntax
like this:

my_hash = { key => value }

Or:

my_hash[key] = value

So frequencies[x] += 1 means for the key x add 1 to its value.

Question 2:

is there any real difference whether you write it as:

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

or

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

I wrote it the second way because that is the way the lesson taught it and it still works but I have seen people write it the first way more often

Answer:

This is done for clarity although I'm not sure it is helpful. Look back to the example code on page 1 and they chose the no-frills syntax:

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

Throughout this course there has been some emphasis on the number of different ways you can do things in Ruby. It helps to be confident with the variations.

Question 3:

frequencies = frequencies.sort_by do |word, count| count
why do we need to make frequencies equal to frequencies.sort_by? Could we not just remove the "frequencies =" portion?

Answer:

I have tried to run the code outside of codecademy. It works with the "frequencies =" and it doesn't work without it. It throws the following error:

Text please:
if i was a rich man
example.rb:8:in <main>': undefined methodreverse!' for {"if"=>1, "i"=>1, "was"=>1, "a"=>1, "rich"=>1, "man"=>1}:Hash (NoMethodError)
Did you mean? reverse_each


#4

Thank you this helped me put the pieces together.


#5

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