Last active
August 29, 2015 14:21
-
-
Save redrick/3d09f5c39d7b4942129b to your computer and use it in GitHub Desktop.
Prize distribution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashMap | |
def initialize | |
@prizes = [35, 25, 20, 15, 5] | |
@participations = [ | |
Participation.new(10), | |
Participation.new(10), | |
Participation.new(10), | |
Participation.new(6), | |
Participation.new(4), | |
Participation.new(4) | |
] | |
@percent = 10 | |
end | |
def mapping | |
# try participations into groups directly without this first array of hashes and then create that... | |
raw_distribution = @participations.map.with_index do |current, i| | |
prize = @prizes[i] ? @prizes[i] * @percent : 0 | |
prize_percent = @prizes[i] ? @prizes[i] : 0 | |
{ position: i+1, participation: current, percentage: prize_percent, prize: prize } | |
end | |
puts '========> before <==========' | |
puts raw_distribution | |
groups = raw_distribution.group_by {|pos| pos[:participation].score } | |
distribution = [] | |
groups.each do |group| | |
recalculated_percentage = 0 | |
group.last.map{ |part| recalculated_percentage += part[:percentage]} | |
recalculated_percentage = recalculated_percentage.to_f / group.last.count | |
group.last.map do |part| | |
part[:percentage] = recalculated_percentage | |
part[:prize] = recalculated_percentage * @percent | |
distribution << part | |
end | |
end | |
puts '========> after <==========' | |
puts distribution | |
end | |
end | |
class Participation | |
attr_accessor :score | |
def initialize score | |
@score = score | |
end | |
end | |
h = HashMap.new | |
h.mapping |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment