【问题标题】:Calculating maximum word frequency in ruby计算ruby中的最大词频
【发布时间】:2015-11-23 21:34:25
【问题描述】:

我被作业的最后一点困住了。我试了几次,但我的代码没有通过测试。有人可以修改并指出我做错了什么吗?

# Implement the calculate_line_with_highest_frequency() method to:
#* calculate the maximum value for highest_wf_count contained by the LineAnalyzer objects in analyzers array
#  and stores this result in the highest_count_across_lines attribute.
#* identifies the LineAnalyzer objects in the analyzers array that have highest_wf_count equal to highest_count_across_lines 
#  attribute value determined previously and stores them in highest_count_words_across_lines.

def calculate_line_with_highest_frequency
  highest_count_across_lines = Hash.new(0)

    @analyzers.each do |line|
      line.content.split.each do |word|
      highest_wf_count[word.downcase] +=1
    end
  end

  @highest_count_across_lines = highest_wf_count.values.max

  @highest_count_words_across_lines = Array.new
  @analyzers.each do |line|
    line.highest_count_across_lines.each do |word|
      @highest_count_words_across_lines << line if highest_count_across_lines[word] == @highest_wf_count
    end
  end

【问题讨论】:

  • 了解您要通过的测试会有所帮助。
  • 如果这是一项作业,最好问问你的教授或导师。您提供的代码没有提供对作业的足够了解。
  • 欢迎来到 Stack Overflow。请阅读“How to Ask”。我们需要更多信息:您的最小输入数据是多少,您需要什么输出?如果代码不起作用,您需要解释您打算让代码做什么以满足要求以及您看到它没有做什么。请不要指望我们会取代你的导师;根据您到目前为止所学的内容,他们更清楚您应该学习什么。我们不能只是跳进去。

标签: ruby-on-rails ruby


【解决方案1】:

我认为你应该在解决方案类中编写初始化方法:

def initialize ()
    @analyzers=Array.new   
end

因此您的分析器已初始化。

【讨论】:

    【解决方案2】:

    我也在为 coursera 做这个作业。我通过了除最后一项之外的所有测试。你通过了测试还是还在做? 我想我们在这方面互相帮助。 请给我您的电子邮件地址

    我没能通过最后的测试

    Solution#calculate_line_with_highest_frequency calculates highest count words across lines to be will, it, really
     Failure/Error: words_found solution.highest_count_words_across_lines.map(&:highest_wf_words).flatten
    NoMethodError:
       undefined method `highest_wf_words' for "really":String
     # ./spec/solution_spec.rb:38:in `map'
     # ./spec/solution_spec.rb:38:in `block (3 levels) in <top (required)>'
    

    【讨论】:

      【解决方案3】:

      如果您仔细阅读该任务,您会注意到highest_count_words_across_lines 变量应该包含具有最多字数的行的 LineAnalyzer 对象。不是文字。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-28
        • 2011-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        相关资源
        最近更新 更多