Created
January 9, 2019 20:25
-
-
Save biske/e7984b28fa395612487778d903cda626 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
class DistributeStudents | |
attr_reader :coaches_to_students_distribution | |
def initialize(coaches, students, coaches_to_students_distribution) | |
@coaches = coaches | |
@students = students | |
@coaches_to_students_distribution = coaches_to_students_distribution | |
@undistributed_students_size = students.size | |
@number_of_students_per_coach = calculate_number_of_students_per_coach | |
end | |
def call | |
@coaches.each do |coach| | |
if @undistributed_students_size > @number_of_students_per_coach | |
@coaches_to_students_distribution[coach] = @number_of_students_per_coach | |
else | |
@coaches_to_students_distribution[coach] = @undistributed_students_size | |
end | |
@undistributed_students_size -= @number_of_students_per_coach | |
end | |
return self | |
end | |
def calculate_number_of_students_per_coach | |
total_students_size = @coaches_to_students_distribution.values.sum + @students.size | |
number_of_students_per_coach = (total_students_size.to_f / @coaches.size.to_f).ceil | |
number_of_students_per_coach -= 1 if number_of_students_per_coach * @coaches.size > total_students_size | |
number_of_students_per_coach | |
end | |
end | |
students = (1..20).to_a | |
coaches = (1..3).to_a | |
coaches_to_students_distribution = {} | |
distribute_students1 = DistributeStudents.new(coaches, students, coaches_to_students_distribution).call | |
puts distribute_students1.coaches_to_students_distribution.inspect | |
new_students = (20..30).to_a | |
distribute_students2 = DistributeStudents.new(coaches, new_students, distribute_students1.coaches_to_students_distribution).call | |
puts distribute_students2.coaches_to_students_distribution.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment