访问数组内的哈希

时间:2019-09-17 00:39:39

标签: ruby-on-rails ruby-on-rails-3

我正在处理一个编码问题,其中我有3行文本,我必须计算出在这些行中出现最多的单词。答案是:['it','really','will'],因为文本是:

这是一个非常非常酷的实验 可爱的小实验 它会起作用吗,也许会起作用吗?您认为会起作用吗?

所有内容都可以在下面的代码中工作,但most_count_words_across_lines方法除外。它应该返回['it','really','will'],但在数组内[{"a"=>1, "cool"=>1, "experiment"=>1, "is"=>1, "really"=>4, "this"=>1}, {"cute"=>1, "experiment"=>1, "little"=>1}, {"do"=>1, "it"=>4, "maybe"=>1, "think"=>1, "will"=>4, "work"=>2, "you"=>1}]返回2个散列。

我尝试遍历具有多个select语句的哈希无用。

这是我到目前为止的完整代码:

class LineAnalyzer
      attr_accessor :highest_wf_count, :highest_wf_words, :content, :line_number #Implement the following read-only attributes in the LineAnalyzer class. 

  def initialize(content, line)   
      @content = content   #* initialize the content and line_number attributes
      @line_number = line   
      @highest_wf_count = 0
      calculate_word_frequency()  
  end

  def calculate_word_frequency()
    @highest_wf_words = Hash.new
    words = @content.downcase.split
    words.each { |w|
      if @highest_wf_words.has_key?(w)
        @highest_wf_words[w] += 1
      else
        @highest_wf_words[w] = 1
      end
    }
    @highest_wf_words.sort_by { |word, count| count }
    @highest_wf_words.each do |key, value|
      if value > @highest_wf_count
        @highest_wf_count = value
      end
    end
  end

  def highest_wf_count= (number)
    @highest_wf_count = number
  end

end


class Solution
  attr_reader :analyzers, :highest_count_across_lines, :highest_count_words_across_lines # Implement the following read-only attributes in the Solution class.

  def initialize() 
    @analyzers = []
    highest_count_across_lines = nil
    highest_count_words_across_lines = []
  end

  def analyze_file()
    File.foreach('test.txt').with_index(1) do |content, line|
      line_analyzer = LineAnalyzer.new(content, line)
      @analyzers << line_analyzer
    end
  end

  def calculate_line_with_highest_frequency()

     @highest_count_across_lines = analyzers.map(&:highest_wf_count).max
     @highest_count_words_across_lines = analyzers.select { |k,v| v = @highest_count_across_lines }
  end


  def print_highest_word_frequency_across_lines() 
    "The following words have the highest frequency per line: \n  #{highest_count_words_across_lines} (appears in line #{line_num} \n"  
  end
end

这是我收到的错误消息:

Failures:

  1) Solution#calculate_line_with_highest_frequency calculates highest count words across lines to be will, it, really
     Failure/Error: expect(words_found).to match_array ["will", "it", "really"]

       expected collection contained:  ["it", "really", "will"]
       actual collection contained:    [{"a"=>1, "cool"=>1, "experiment"=>1, "is"=>1, "really"=>4, "this"=>1}, {"cute"=>1, "experiment"=>1, "little"=>1}, {"do"=>1, "it"=>4, "maybe"=>1, "think"=>1, "will"=>4, "work"=>2, "you"=>1}]
       the missing elements were:      ["it", "really", "will"]
       the extra elements were:        [{"a"=>1, "cool"=>1, "experiment"=>1, "is"=>1, "really"=>4, "this"=>1}, {"cute"=>1, "experiment"=>1, "little"=>1}, {"do"=>1, "it"=>4, "maybe"=>1, "think"=>1, "will"=>4, "work"=>2, "you"=>1}]
     # ./spec/solution_spec.rb:39:in `block (3 levels) in <top (required)>'

Finished in 0.26418 seconds (files took 0.38 seconds to load)
19 examples, 1 failure

Failed examples:

rspec ./spec/solution_spec.rb:31 # Solution#calculate_line_with_highest_frequency calculates highest count words across lines to be will, it, really

我尝试遍历数组中的哈希值,但一直收到错误消息。我试图找到值(计数)等于最高计数(4)的键。因此,最终答案应为[“ it”,“ really”,“ will”]。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

第1步

将哈希数组合并为一个哈希: 可以说,您的哈希数组为arrayOfHash

hash = arrayOfHash.inject(:merge)

第2步

收集我们在步骤1中创建的单个hash中包含最大值的键。

result = arrayOfHash.collect{|k, v| k if v == arrayOfHash.values.max}.compact