Joseph - 9 days ago 5

jQuery Question

I'm working on a school programming project, and I need to assign each student a group, so that all groups have approximately the same number of boys as girls.

In addition the old students keep the same group, so I initially have 2 groups (A and B) with some students already assigned and a list to assign the group. Each group has a maximum number of students allowed.

For example:

-Numbers of students previously assigned:

`Group A Group B`

-------------- ---------------

Boys 6 Boys 12

Girls 9 Girls 8

-------------- ---------------

Total 15 Total 20

-Numbers of students to assign:

`-------------- ---------------`

Boys 8 Girls 4

-------------- ---------------

-Maximum number of students per group:

`------------------------------------------`

25 students per group

------------------------------------------

-Solution to be obtained:

`Group A Group B`

-------------- ---------------

Boys 13 Boys 13

Girls 11 Girls 10

-------------- ---------------

Total 24 Total 23

-The process I followed to reach this solution is as follows:

`1. I have calculated the total number of boys and girls:`

Total Boys: 6 + 12 + 8 = 26

Total Girls: 9 + 8 + 4 = 21

2. I have calculated half of both amounts:

Total Boys: 26 / 2 = 13

Total Girls: 21 / 2 = 10 (10.5)

3. I have calculated the difference between the students assigned and the

students remaining to be assigned until the amount obtained previously:

Group A Group B

-------------- ---------------

Boys 13-6= 7 Boys 13-12= 1

Girls 10-9= 1 Girls 10-8= 2

-------------- ---------------

4. I have assigned the necessary amount of the students in each group:

Group A Group B

-------------- ---------------

Boys 6+7= 13 Boys 12+1= 13

Girls 9+1= 10 Girls 8+2= 10

-------------- ---------------

Total 23 Total 23

5. And finally the one that I have left I have added it to the first

group:

Group A Group B

-------------- ---------------

Boys 13 Boys 13

Girls 10+1=11 Girls 10

-------------- ---------------

Total 24 Total 23

I need to know how to make the assignment to be valid for any number of previously assigned students. I also need to do the same process with three groups (A, B and C).

Thanks in advance

Answer

The solution you propose is good one.
Just please remeber to divide the number of students by the number of groups in `step #2`

. That should be enough to make your solution flexible.

Source (Stackoverflow)

Comments