Dan*_*ell 4 ruby arrays string hash
我是Ruby的新手,并尝试编写一个方法,该方法将返回字符串中最常见的单词数组.如果有一个具有高计数的单词,则应返回该单词.如果高计数绑定了两个单词,则两者都应以数组形式返回.
问题是,当我通过第二个字符串时,代码只计算"单词"两次而不是三次.当第三个字符串通过时,它返回"it",计数为2,这没有任何意义,因为"它"的计数应为1.
def most_common(string)
counts = {}
words = string.downcase.tr(",.?!",'').split(' ')
words.uniq.each do |word|
counts[word] = 0
end
words.each do |word|
counts[word] = string.scan(word).count
end
max_quantity = counts.values.max
max_words = counts.select { |k, v| v == max_quantity }.keys
puts max_words
end
most_common('a short list of words with some words') #['words']
most_common('Words in a short, short words, lists of words!') #['words']
most_common('a short list of words with some short words in it') #['words', 'short']
Run Code Online (Sandbox Code Playgroud)
计算单词实例的方法是你的问题. it是的with,所以它被重复计算.
[1] pry(main)> 'with some words in it'.scan('it')
=> ["it", "it"]
Run Code Online (Sandbox Code Playgroud)
它可以更容易地完成,您可以使用each_with_object调用按值的实例数对数组的内容进行分组,如下所示:
counts = words.each_with_object(Hash.new(0)) { |e, h| h[e] += 1 }
Run Code Online (Sandbox Code Playgroud)
这将遍历数组中的每个条目,并为散列中每个单词的条目的值加1.
所以以下内容适合您:
def most_common(string)
words = string.downcase.tr(",.?!",'').split(' ')
counts = words.each_with_object(Hash.new(0)) { |e, h| h[e] += 1 }
max_quantity = counts.values.max
counts.select { |k, v| v == max_quantity }.keys
end
p most_common('a short list of words with some words') #['words']
p most_common('Words in a short, short words, lists of words!') #['words']
p most_common('a short list of words with some short words in it') #['words', 'short']
Run Code Online (Sandbox Code Playgroud)