Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Customer
- attr_accessor :id, :size, :candidates
- def initialize id, size
- @id = id.to_i
- @size = size.to_i
- @candidates = []
- end
- end
- class Attendant
- attr_accessor :id, :from, :to, :range
- def initialize id, level
- @id = id.to_i
- @from = 0
- @to = level.to_i
- end
- def range size
- return size.between? @from, @to
- end
- end
- class DistributeCustomer
- def initialize(attendants, customers, unavailable_attendants)
- @unavailable_attendants = unavailable_attendants
- createAttendants attendants.sort_by {|k,v| v}.reverse
- createCustomers customers.sort_by {|k,v| v}.reverse
- createCandidates
- distribute
- winner
- end
- def createAttendants rawAttendants
- @attendants = []
- rawAttendants.each do |attendant|
- a = Attendant.new(attendant[0], attendant[1])
- @attendants << a
- end
- self.removeUnavailable
- self.attendantsRange
- end
- def createCustomers rawCustomers
- @customers = []
- rawCustomers.each do | customer |
- c = Customer.new(customer[0], customer[1])
- @customers << c
- end
- end
- def attendantsRange
- i = 0
- @attendants.each do | attendant |
- if @attendants[i+1] and @attendants[i+1].to < attendant.to
- attendant.from = @attendants[i+1].to
- else
- attendant.from = 0
- end
- i += 1
- end
- end
- def removeUnavailable
- i = 0
- @attendants.each do |attendant|
- @attendants.delete_at(i) if @unavailable_attendants.include? attendant.id
- i += 1
- end
- end
- def createCandidates
- @customers.each do |customer|
- candidates = []
- @attendants.each do |me|
- if me.range customer.size
- candidates << me
- end
- end
- customer.candidates = candidates.uniq
- end
- end
- def distribute
- @distributed = []
- @customers.each do |customer|
- if customer.candidates.length == 1
- distributed = customer.candidates[0].id
- @distributed << distributed
- else
- if @distributed.include? customer.candidates[0].id
- if @distributed.include? customer.candidates[1].id
- if @distributed.count(customer.candidates[0].id) > @distributed.count(customer.candidates[1].id)
- @distributed << customer.candidates[1].id
- elsif @distributed.count(customer.candidates[0].id) < @distributed.count(customer.candidates[1].id)
- @distributed << customer.candidates[0].id
- else
- @distributed << customer.candidates[0].id
- end
- else
- @distributed << customer.candidates[1].id
- end
- else
- @distributed << customer.candidates[0].id
- end
- end
- end
- end
- def winner
- winner = @distributed.sort_by { |id, hits| [hits, id] }.reverse
- p winner.first[0]
- end
- end
- attendants = {1=>'60', 2=>'20', 3=>'95', 4=>'75'}
- customers = {1=>'90', 2=>'20', 3 =>'70', 4=>'40', 5=>'60', 6=>'10'}
- unavailable_attendants = [2, 4]
- d= DistributeCustomer.new(attendants, customers, unavailable_attendants)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement