From 8accdad04152b844f9b7b1487ed3c7948151286a Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Sat, 16 May 2015 00:44:19 +0900 Subject: [PATCH] Add variance in score board. --- app/models/group.rb | 35 ++++++++++++++++++++----- app/views/contests/scores/show.html.erb | 2 ++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/models/group.rb b/app/models/group.rb index f470e71..06c1560 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -24,10 +24,10 @@ def attendances_for(contest) def solved_submission_for(problem, type) Submission.where( - problem_id: problem.id, - problem_type: type, - solved: true, - attendance_id: Attendance.where(user_id: user_ids).select(:id)) + problem_id: problem.id, + problem_type: type, + solved: true, + attendance_id: Attendance.where(user_id: user_ids).select(:id)) .order("score DESC") .first end @@ -39,13 +39,34 @@ def solved?(problem, type, attendances = attendances_for(problem.contest)) def score_for(contest) Submission.where( - problem_id: contest.problem_ids, - solved: true, - attendance_id: Attendance.where(user_id: user_ids).select(:id)) + problem_id: contest.problem_ids, + solved: true, + attendance_id: Attendance.where(user_id: user_ids).select(:id)) .group(:problem_id, :problem_type) .maximum(:score) .values .sum end + def variance_for(contest) + group = Submission.where( + problem_id: contest.problem_ids, + solved: true, + attendance_id: Attendance.where(user_id: user_ids).select(:id)) + .group(:problem_id, :problem_type) + .maximum(:score) + uid2score = Hash[user_ids.map { |id| [id, 0] }] + group.each do |k, v| + s = Submission.where( + problem_id: k[0], + problem_type: k[1], + score: v, + solved: true, + attendance_id: Attendance.where(user_id: user_ids).select(:id)).first + uid2score[s.attendance.user_id] += s.score + end + avg = uid2score.map { |uid, score| score }.sum.to_f / uid2score.size + uid2score.map { |uid, score| (score - avg) * (score - avg) }.sum / uid2score.size + end + end diff --git a/app/views/contests/scores/show.html.erb b/app/views/contests/scores/show.html.erb index 1ddec3c..1ff08ca 100644 --- a/app/views/contests/scores/show.html.erb +++ b/app/views/contests/scores/show.html.erb @@ -7,6 +7,7 @@ Place Team Name Score + Variance <% @problems.each do |problem| %> <%= tooltip problem.title do %> @@ -30,6 +31,7 @@ <%= index + 1 %> <%= group.name %> <%= group.score_for(@contest) %> + <%= group.variance_for(@contest).round(2) %> <% @problems.each do |problem| %>