【问题标题】:Doing a calculation on two Ruby multi-dimensional arrays对两个 Ruby 多维数组进行计算
【发布时间】:2020-10-18 03:51:30
【问题描述】:

这是为了我自己的教育成长,因为我的代码有效,但我确信有更好的编码方式。我一直在查找各种 Ruby .map、.inspect 等,但似乎无法在网上找到可以理解的解释。这就是我想要做的。

我有两个数组。第一个数组有多个部分,每个部分有两个值。它们取自将工作职责映射到工作职位的表格,并且该地图具有该职责对该职位的权重。因此,例如,知道如何编码的工作职责对于程序员来说是 10,而对于办公室助理来说是 1。

第二个数组有多个部分,每个部分有两个值。它们取自一张表格,该表格将员工的答案映射到他们认为自己适合该责任的程度。

我通过这段代码得到了第一个数组:

@responsibility_weights = ResponsibilityMap.where(job_position_id: job_position_id).pluck(:id, :weight)
 => [[4, 10], [5, 8], [6, 9]] 

如您所见,这会为给定职位的职责创建一个数组,并用responsibility_map 的ID 和它在该职位上的权重填充数组。

我通过这段代码得到第二个数组:

@responsibility_answers = ResponsibilityAnswerMap.where(application_id: application_id).pluck(:responsibility_map_id, :answer)
 => [[4, 4], [5, 3], [6, 3]] 

如您所见,这会为员工的给定答案创建一个数组,并用responsibility_map 的ID 和给定答案填充该数组。

查看这两个数组,你应该看到它们填充了 3 个组,每个组的 ID 与第一个数字相同。

员工以 1-5 的比例衡量自己,1 表示完全没有,5 表示最好。所以,我认为最好将问题的权重除以 5,得到每个排名应该得到多少分,然后乘以他们给出的答案,得到他们应该为该责任得到多少分的结果.然后,我想将他们获得的所有积分相加,然后除以可以获得的总积分,以获得他们在该工作职位中排名的百分比。一清二楚了吗?我希望我解释得很好。

**问题:**

这就是我的问题所在。我在下面列出的代码可以工作并且可以完成工作。但是,我相信有一种更简洁的方法可以得到结果。谁能告诉我和任何未来的网络搜索者你会怎么做? (我希望我提供了足够的信息来点击关键字,以便任何未来的网络搜索者找到这个问题和相关答案。)

注意:此代码确实包含多个员工申请的数组,计算成绩,然后使用成绩更新应用程序。以防万一有人看到这个并想知道代码的那些部分。

def calculate_responsibility_grade(job_position_id, applications_array) 
    calculate_responsibility_grade = false

    @responsibility_weights = ResponsibilityMap.where(job_position_id: job_position_id).pluck(:id, :weight)

    #This is type of concise coding I am trying to find out how to use below
    @total_weight = @responsibility_weights.map{ |key, value| value}.sum 

    i = 0
    loop do
        if applications_array[i].nil?
            break
        end

        #Grabs new employy answers
        @responsibility_answers = ResponsibilityAnswerMap.where(application_id: applications_array[i]).pluck(:responsibility_map_id, :answer)

        #Resets scores to 0 from first previous run through    
        @score = 0
        @total_score = 0
        @responsibility_weights.each do | key1, weight|
            @responsibility_answers.each do | key2, answer|
                if key1 == key2     # Checks if the two keys match meaning they are for the same responsibility
                    @score = (weight/5.to_f) * answer    # Divides the weight by the number raking (1-5) to get the value of each rank, then multiplies by the answer the employee gave
                    @total_score = @newScore + @score  # Adds the score for that answer to total score
                end
            end
        end
        
        responsibility_grade = (@total_score/@total_weight) * 100  # Gets percentage

        # Updates application with new grade.
        Application.find(applications_array[i]).update(responsibility_grade: responsibility_grade)

        i = i + 1
    end
    
    calculate_responsibility_grade = true
end

提前感谢您花时间教育我和未来的研究人员。

【问题讨论】:

  • 这对 codereview.stackexchange.com 来说是一个更好的问题。另外,我认为你的@responsibility_answers = ResponsibilityAnswerMap.where(application_id: applications_array[i]).pluck(:responsibility_map_id, :answer) 行不通。我想你只需要在这里采摘responsibility_map_id
  • 这闻起来像是您应该在数据库中创建的那种聚合,而不是通过 Ruby/任何语言循环访问数据。

标签: arrays ruby-on-rails ruby hash activerecord-calculations


【解决方案1】:

一些提示,不是完全重新实现:

  1. 将数据库访问与逻辑分开
# fetch from DB up here

# let's say these are the values we get
weights = [[4, 10], [5, 8], [6, 9]]
answers = [[4, 4], [5, 3], [6, 3]]

# Logic only here, no DB
# ...
# ...

# let's say these are the results we get
application_grades = { 5 => 12.3, 7 => 32.4 }

# now do DB writes
  1. 拆分操作,示例如下
# associate weights to answers
id_to_weights = weights.to_h
answers_to_weights = answers.map do |id, answer|
  id_to_weights[i] ? [answer, id_to_weights[i]] : nil
end.compact.to_h

# compute answer scores
answer_scores = answers_to_weights.map { |answer, weight| weight * answer / 5.0 }
# sum all answer scores
total_score = answer_scores.sum
# normalize weights
grade = total_score.to_f / id_to_weights.values.sum * 100

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多