Create a Histogram

I Data Structures, Meet Iteration

i. What you’ll be building 1/8

Save and Submit

ii. You Know the Drill 2/8

puts "Histogram?"
text = gets.chomp

iii. Building the Words Array 3/8

puts "Histogram?"
text = gets.chomp
words = text.split()

iv. Creating the Frequencies Hash 4/8

puts "Histogram?"
text = gets.chomp
words = text.split()
frequencies = Hash.new(0)

v. Iterating Over the Array 5/8

puts "Histogram?"
text = gets.chomp
words = text.split()
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1}

vi. Sorting the Hash 6/8

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

vii. Iterating Over the Hash 7/8

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

viii. You Did It! 8/8

Save and Submit!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s