Created
August 26, 2019 01:48
-
-
Save taiar/062f56c43c4a5897e8452e07d12edfe7 to your computer and use it in GitHub Desktop.
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
| css = [[1, 60] ,[3, 95]] | |
| customers = [[1, 90], [2, 20], [3, 70], [4, 40], [5, 60], [6, 10]] | |
| vacant_css = [2, 4] | |
| def can_answer?(cs, customer) | |
| cs[1] >= customer[1] | |
| end | |
| def most_free(candidates, allocation) | |
| counter = {} | |
| relevant_allocation = allocation.select{ |a| candidates.include? a[0] } | |
| candidates.each do |c| | |
| counter[c] = 0 if counter[c].nil? | |
| relevant_allocation.each do |a| | |
| counter[c] += 1 if a[0] == c | |
| end | |
| end | |
| counter.sort_by{ |k, v| v }.first[0] | |
| end | |
| def csRush(css, customers, vacant_css) | |
| active_cs_agents = css.select{ |k, v| !vacant_css.include? k }.sort_by{ |k, v| v }.reverse! | |
| customers.sort_by!{ |k, v| v }.reverse! | |
| allocation = [] | |
| customers.each do |customer| | |
| css_able_to_answer = active_cs_agents.select{ |cs| can_answer?(cs, customer) }.map{ |cs| cs[0] } | |
| answerer = most_free(css_able_to_answer, allocation) | |
| allocation << [answerer, customer[0]] | |
| end | |
| counter = {} | |
| allocation.each do |a| | |
| counter[a[0]] = 0 if counter[a[0]].nil? | |
| counter[a[0]] += 1 | |
| end | |
| counter = counter.sort_by{ |k, v| -v } | |
| return counter[0][1] == counter[1][1] ? 0 : counter[0][0] | |
| end | |
| puts csRush(css, customers, vacant_css) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment