SHARE
TWEET

Untitled

a guest May 22nd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Customer
  2.   attr_accessor :id, :size, :candidates
  3.   def initialize id, size
  4.     @id = id.to_i
  5.     @size = size.to_i
  6.     @candidates = []
  7.   end
  8. end
  9.  
  10. class Attendant
  11.   attr_accessor :id, :from, :to, :range
  12.   def initialize id, level
  13.     @id = id.to_i
  14.     @from = 0
  15.     @to = level.to_i
  16.   end
  17.  
  18.   def range size
  19.     return size.between? @from, @to
  20.   end
  21. end
  22.  
  23. class DistributeCustomer
  24.  
  25.   def initialize(attendants, customers, unavailable_attendants)
  26.       @unavailable_attendants = unavailable_attendants
  27.       createAttendants attendants.sort_by {|k,v| v}.reverse
  28.       createCustomers customers.sort_by {|k,v| v}.reverse
  29.       createCandidates
  30.       distribute
  31.       winner
  32.   end
  33.  
  34.   def createAttendants rawAttendants
  35.     @attendants = []
  36.     rawAttendants.each do |attendant|
  37.       a = Attendant.new(attendant[0], attendant[1])
  38.       @attendants << a
  39.     end
  40.     self.removeUnavailable
  41.     self.attendantsRange
  42.   end
  43.  
  44.   def createCustomers rawCustomers
  45.     @customers = []
  46.     rawCustomers.each do | customer |
  47.       c = Customer.new(customer[0], customer[1])
  48.       @customers << c
  49.     end
  50.  
  51.   end
  52.  
  53.   def attendantsRange
  54.     i = 0
  55.     @attendants.each do | attendant |
  56.       if @attendants[i+1] and @attendants[i+1].to < attendant.to
  57.         attendant.from = @attendants[i+1].to
  58.       else
  59.         attendant.from = 0
  60.       end
  61.       i += 1
  62.     end
  63.   end
  64.  
  65.   def removeUnavailable
  66.     i = 0
  67.     @attendants.each do |attendant|
  68.       @attendants.delete_at(i) if @unavailable_attendants.include? attendant.id
  69.       i += 1
  70.     end
  71.   end
  72.  
  73.   def createCandidates
  74.     @customers.each do |customer|
  75.       candidates = []
  76.       @attendants.each do |me|
  77.         if me.range customer.size
  78.           candidates << me
  79.         end
  80.       end
  81.       customer.candidates = candidates.uniq
  82.     end
  83.   end
  84.  
  85.   def distribute
  86.     @distributed = []
  87.  
  88.     @customers.each do |customer|
  89.  
  90.       if customer.candidates.length == 1
  91.         distributed = customer.candidates[0].id
  92.         @distributed << distributed
  93.       else
  94.         if @distributed.include? customer.candidates[0].id
  95.           if @distributed.include? customer.candidates[1].id
  96.             if @distributed.count(customer.candidates[0].id) > @distributed.count(customer.candidates[1].id)
  97.               @distributed << customer.candidates[1].id
  98.             elsif @distributed.count(customer.candidates[0].id) < @distributed.count(customer.candidates[1].id)
  99.               @distributed << customer.candidates[0].id
  100.             else
  101.               @distributed << customer.candidates[0].id
  102.             end
  103.           else
  104.             @distributed << customer.candidates[1].id
  105.           end
  106.         else
  107.           @distributed << customer.candidates[0].id
  108.         end
  109.       end
  110.     end
  111.   end
  112.  
  113.   def winner
  114.     winner = @distributed.sort_by { |id, hits| [hits, id] }.reverse
  115.     p winner.first[0]
  116.   end
  117.  
  118. end
  119.  
  120.  
  121.  
  122. attendants = {1=>'60', 2=>'20', 3=>'95', 4=>'75'}
  123. customers = {1=>'90', 2=>'20', 3 =>'70', 4=>'40', 5=>'60', 6=>'10'}
  124. unavailable_attendants = [2, 4]
  125.  
  126. d= DistributeCustomer.new(attendants, customers, unavailable_attendants)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top