There is a teacher counter (int x = 11) and days in the selected month (int month = 29).

I want to accidentally add teachers a day at a time at a minimum to repeat teachers

For example, I have: 29 days and 11 teachers

for this, example 4 teachers must teach two days of lesson per month and 7 teachers must teach three days per month

and the result should be:

4 Teacher * 2 lessons = 8

7 Teacher * 3 lessons = 21

Total: 29

I want the automatic calculated and rightly put the days in the table. At a minimum, I need an idea or algorithm to solve this problem.

enter image description here

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Algorithm

Let the array of teachers $names and the number of days $days in the current month be given.

The solution can be built on the function random_slice($arr, $num) returns a random sample of length num without repetitions from the arr array — as if we wrote the elements of the array on pieces of paper and alternately draw them num times.

Programmatically it is implemented as:

  1. We calculate $cnt as the dimension of the $arr array.

  2. Repeat $num times:
    2.1. Randomly select the index $i in the range 0...$cnt-1 .
    2.2. $arr[$i] written to the $result array with the next number.
    2.3. Reduce $cnt by 1.
    2.4. $arr[$cnt] write $arr[$i] into place.

  3. result - the required sample.

Now enough:

  1. Randomly select teachers for an extra 7 days $list = random_slice($names, 7) .
  2. Supplement it with the names array to the required length 29 .
  3. Randomly create a graph $shedule = random_slice($list, 29)

PHP program text:

 $names = ["Name01", "Name02", "Name03", "Name04","Name05", "Name06", "Name07", "Name08", "Name09", "Name10", "Name11"]; $days = 29; print "days = $days<br>names:"; var_dump($names); function random_slice($arr, $num){ $result = []; $cnt = count($arr); for($n = 0; $n < $num; $n++){ $index = mt_rand(0, --$cnt); $result[] = $arr[$index]; $arr{$index} = $arr[$cnt]; } return $result; } $num = $days % count($names); $list = random_slice($names, $num); print "num = $num<br>list:"; var_dump($list); while(count($list) < $days){ $list = array_merge($list, $names); } print "merged list:"; var_dump($list); $shedule = random_slice($list, $days); print "shedule:"; var_dump($shedule); 

Results:

 days = 29 names: array (size=11) 0 => string 'Name01' (length=6) 1 => string 'Name02' (length=6) 2 => string 'Name03' (length=6) 3 => string 'Name04' (length=6) 4 => string 'Name05' (length=6) 5 => string 'Name06' (length=6) 6 => string 'Name07' (length=6) 7 => string 'Name08' (length=6) 8 => string 'Name09' (length=6) 9 => string 'Name10' (length=6) 10 => string 'Name11' (length=6) num = 7 list: array (size=7) 0 => string 'Name03' (length=6) 1 => string 'Name08' (length=6) 2 => string 'Name07' (length=6) 3 => string 'Name01' (length=6) 4 => string 'Name06' (length=6) 5 => string 'Name02' (length=6) 6 => string 'Name10' (length=6) merged list: array (size=29) 0 => string 'Name03' (length=6) 1 => string 'Name08' (length=6) 2 => string 'Name07' (length=6) 3 => string 'Name01' (length=6) 4 => string 'Name06' (length=6) 5 => string 'Name02' (length=6) 6 => string 'Name10' (length=6) 7 => string 'Name01' (length=6) 8 => string 'Name02' (length=6) 9 => string 'Name03' (length=6) 10 => string 'Name04' (length=6) 11 => string 'Name05' (length=6) 12 => string 'Name06' (length=6) 13 => string 'Name07' (length=6) 14 => string 'Name08' (length=6) 15 => string 'Name09' (length=6) 16 => string 'Name10' (length=6) 17 => string 'Name11' (length=6) 18 => string 'Name01' (length=6) 19 => string 'Name02' (length=6) 20 => string 'Name03' (length=6) 21 => string 'Name04' (length=6) 22 => string 'Name05' (length=6) 23 => string 'Name06' (length=6) 24 => string 'Name07' (length=6) 25 => string 'Name08' (length=6) 26 => string 'Name09' (length=6) 27 => string 'Name10' (length=6) 28 => string 'Name11' (length=6) shedule: array (size=29) 0 => string 'Name07' (length=6) 1 => string 'Name08' (length=6) 2 => string 'Name01' (length=6) 3 => string 'Name02' (length=6) 4 => string 'Name07' (length=6) 5 => string 'Name04' (length=6) 6 => string 'Name10' (length=6) 7 => string 'Name11' (length=6) 8 => string 'Name10' (length=6) 9 => string 'Name07' (length=6) 10 => string 'Name09' (length=6) 11 => string 'Name04' (length=6) 12 => string 'Name05' (length=6) 13 => string 'Name03' (length=6) 14 => string 'Name01' (length=6) 15 => string 'Name08' (length=6) 16 => string 'Name06' (length=6) 17 => string 'Name05' (length=6) 18 => string 'Name10' (length=6) 19 => string 'Name09' (length=6) 20 => string 'Name08' (length=6) 21 => string 'Name01' (length=6) 22 => string 'Name03' (length=6) 23 => string 'Name03' (length=6) 24 => string 'Name11' (length=6) 25 => string 'Name06' (length=6) 26 => string 'Name02' (length=6) 27 => string 'Name02' (length=6) 28 => string 'Name06' (length=6) 

    The easiest method

    1. Create an array with 29 elements
    2. Add account IDs as many times as a lesson. for 4 teachers, their IDs are added to this array 2 times, and for 7 teachers, 3 times.
    3. mix this array. It is necessary to rearrange the elements randomly.
    4. Optionally, you can add a check for the next identical ID. What would 2 ID in a row did not come. if so, swap 2nd identical ID followed by
    5. you have an array ready. each array index is a day on the calendar

    Option number 2

    show in the example

    1) there is data

    4 Teacher * 2 lessons = 8 - let them be (1,2,3,4)

    7 Teacher * 3 lessons = 21 - let them be (5,6,7,8,9,10,11)

    2) create 2 arrays from A [4] and B [7] elements. Fill the first with elements (1,2,3,4) and the second with (5,6,7,8,9,10,11)

    3) create arrays C [8] and D [21]

    4) Shuffle array A and add it to array C. Do it 2 times. We also do for arrays B and D only 3 times.

    For example:

     A[] = {1,2,3,4} A[перемешка] = {4,1,3,2} C[] = {4,1,3,2} A[перемешка] = {2,4,3,1} C[] = {4,1,3,2,2,4,3,1} B[] = {5,6,7,8,9,10,11} B[перемешка] = {5,6,7,8,9,10,11} D[] = {5,6,7,8,9,10,11} B[перемешка] = {9,10,5,6,7,8,11} D[] = {5,6,7,8,9,10,11,9,10,5,6,7,8,11} B[перемешка] = {8,9,10,11,5,6,7} D[] = {5,6,7,8,9,10,11,9,10,5,6,7,8,11,8,9,10,11,5,6,7} 

    3) now need to collect. for this we need an array of X [29] now we take randomly from 2 arrays and add an array of X []

    Example

     C[] = {4,1,3,2,2,4,3,1} D[] = {5,6,7,8,9,10,11,9,10,5,6,7,8,11,8,9,10,11,5,6,7} X[] = {5,6,4,7,1,3,8,2,9,10,2,11,9,4,10,5,6,7,3,8,11,8,9,1,10,11,5,6,7} 
    • I'm a little confused. can i have code example - Kesha
    • 2
      @Kesha code will not. You asked the algorithm - Saidolim
    • The choice of teachers for 2 lessons and for 3 lessons should also be random - Yuri Negometyanov