Skip to content

Commit

Permalink
Add variance in score board.
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed May 15, 2015
1 parent 11af05f commit 8accdad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
35 changes: 28 additions & 7 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 2 additions & 0 deletions app/views/contests/scores/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<th class='center'>Place</th>
<th>Team Name</th>
<th class='center'>Score</th>
<th class='center'>Variance</th>
<% @problems.each do |problem| %>
<th class='center'>
<%= tooltip problem.title do %>
Expand All @@ -30,6 +31,7 @@
<td class='center'><%= index + 1 %></td>
<td><%= group.name %></td>
<td class='center'><%= group.score_for(@contest) %></td>
<td class='center'><%= group.variance_for(@contest).round(2) %></td>
<% @problems.each do |problem| %>
<td class='center'>
<div>
Expand Down

0 comments on commit 8accdad

Please sign in to comment.