Write a ruby program to find the number of occurrences of a given word in a string. I managed to come up with this solution.
class count
def words(string)
words = string.downcase.split(/[\s,]+/).map {|w| remove_punctuation(w)}
frequency = Hash.new(0)
words.each do |word|
frequency[word] += 1
end
return frequency
end
but I need the format to be
expected: [["love", 3], ["one", 1], ["the", 2], ["red", 1],
["to", 4], ["light", 1] , ["green", 1], ["leads", 3],, ["all", 2],
["you", 1]]
obteve: {"light" => 1, "love" => 3, "all" => 2, "one" => 1, "leads" => 3, "red" => 1, "green" => 1, "o" => 2, "para" => 4, "você" => 1}
E deixe-me mostrar a você as informações na ordem em que esta frase está, com meu resultado aparecendo em uma ordem totalmente diferente.
Get Answers For Free
Most questions answered within 1 hours.