Skip to content

Commit

Permalink
Add a calculation method for user's score.
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed May 25, 2015
1 parent 8accdad commit d647d2c
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,40 @@ def score_for(contest)
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
uid2score = create_uid2score_for(contest)
avg = uid2score.map { |uid, score| score }.sum.to_f / uid2score.size
uid2score.map { |uid, score| (score - avg) * (score - avg) }.sum / uid2score.size
end

def score_of_user_for(contest, user)
uid2score = create_uid2score_for(contest)
uid2score[user.id]
end

private

def create_uid2score_for(contest)
unless @uid2score
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))
.order(:created_at)
.first
@uid2score[s.attendance.user_id] += s.score
end
end
@uid2score
end

end

0 comments on commit d647d2c

Please sign in to comment.