通过附加要求扩展阵列

我是 PHP 新手,但我会尽力而为。请耐心等待我。:)

昨天我已经问过类似的问题,但今天我必须扩展它。

初始情况

在我的学校,毕业生必须在最后一年选择论文主题。每个学生从一个学科领域选择一位导师来帮助他们。

起始位置

  • 每个学生必须准确指定三个愿望,并按偏好降序排列。示例:史黛西在设计上选择乔布斯先生作为她的第一愿望,卡尔也选择乔布斯先生作为他的第一愿望,但在可用性方面。威廉选择计算机科学专业的盖茨先生作为他的第一选择,夏洛特选择生物学专业的盖茨先生作为他的第一选择。

  • 每个导师最多可以指导三名学生。

  • 每位教师最多可以选择两个科目。因此,可以在设计和可用性方面选择乔布斯先生,在计算机科学和生物学方面选择盖茨先生,在物理和地理学方面选择马斯克先生。

可能的选举示例:

Mr. Jobs -> Stacy (Design), Carl (Usability), Melody (Design)
Mr. Gates -> William (Computer Science), Eric (Biology), Charlott (Biology)
Mr. Musk -> Anthony (Physics), Sarah (Physics), Nelly (Geography)

最后的问题

  • 我怎样才能确保尽可能多的学生实现他们的第一个愿望?剩下的学生中,尽可能多地实现第二个愿望,其余的实现第三个愿望。(完成了吗?有什么建议吗?)

  • 我如何扩展以下脚本,以便考虑同事的不同科目,但没有人监督超过 3 个学生?

  • 我怎样才能告诉脚本也输出分配的是第一个愿望、第二个愿望还是第三个愿望?

用户Rustyjim昨天帮助我编写了以下脚本。再次感谢!

祝您身体健康!

到目前为止的脚本

function shuffle_assoc($list) {

    if (!is_array($list)) return $list;

    $keys = array_keys($list);

    shuffle($keys);

    $random = array();

    foreach ($keys as $key) {

        $random[$key] = $list[$key];

    }

    return $random;

}

$preferencesOfStudents = [

    'students' => [

        'Stacy' => ['Mr Jobs','Mr Gates','Mr Musk'],

        'Carl' => ['Mr Jobs','Mr Gates','Mr Musk'],

        'Melody' => ['Mr Jobs','Mr Musk','Mr Gates'],

        'William' => ['Mr Musk','Mr Gates','Mr Jobs'],

        'Eric' => ['Mr Gates','Mr Jobs','Mr Musk'],

        'Charlott' => ['Mr Jobs','Mr Gates','Mr Musk'],

        'Anthony' => ['Mr Gates','Mr Musk','Mr Jobs'],

        'Sarah' => ['Mr Gates','Mr Jobs','Mr Musk'],

        'Nelly' => ['Mr Gates','Mr Musk','Mr Jobs']

    ]

];

// 1 = Jobs, 2 = Gates, 3 = Musk

$teachers = [

    'Mr Jobs' => [],

    'Mr Gates' => [],

    'Mr Musk' => []

];

$randomStudentsArray = shuffle_assoc($preferencesOfStudents['students']);

//print_r($randomStudentsArray);

foreach($randomStudentsArray as $name => $student){

    if(count($teachers[$student[0]]) < 3){

        $teachers[$student[0]][] = $name;

    } elseif(count($teachers[$student[1]]) < 3) {

        $teachers[$student[1]][] = $name;

    } else {

        $teachers[$student[2]][] = $name;

    }

}

print_r($teachers);


繁星coding
浏览 103回答 1
1回答

MMTTMM

编辑:这有效吗?希望能有所帮助!如果最多有 2 个专业,则会出现一个问题:有时并非所有学生都可以分配。但它会尽可能地尝试,而且是随机的,所以如果它没有给出好的输出,就再试一次。&nbsp; &nbsp;<?phpfunction shuffle_assoc($list) {&nbsp; &nbsp; if (!is_array($list)) return $list;&nbsp; &nbsp; $keys = array_keys($list);&nbsp; &nbsp; shuffle($keys);&nbsp; &nbsp; $random = array();&nbsp; &nbsp; foreach ($keys as $key) {&nbsp; &nbsp; &nbsp; &nbsp; $random[$key] = $list[$key];&nbsp; &nbsp; }&nbsp; &nbsp; return $random;}function searchForId($id, $array) {&nbsp; &nbsp; foreach ($array as $key => $val) {&nbsp; &nbsp; &nbsp; &nbsp; if ($val['specializations'] === $id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $key;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return null;&nbsp;}$preferencesOfStudents = [&nbsp; &nbsp; 'students' => [&nbsp; &nbsp; &nbsp; &nbsp; 'Stacy' => ['Mr Jobs','Mr Gates','Mr Musk', 'Biology'],&nbsp; &nbsp; &nbsp; &nbsp; 'Carl' => ['Mr Jobs','Mr Gates','Mr Musk', 'Design'],&nbsp; &nbsp; &nbsp; &nbsp; 'Melody' => ['Mr Bezos','Mr Musk','Mr Gates', 'Usability'],&nbsp; &nbsp; &nbsp; &nbsp; 'William' => ['Mr Musk','Mr Gates','Mr Jobs', 'Computer Science'],&nbsp; &nbsp; &nbsp; &nbsp; 'Eric' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics'],&nbsp; &nbsp; &nbsp; &nbsp; 'Charlott' => ['Mr Jobs','Mr Gates','Mr Musk', 'Geography'],&nbsp; &nbsp; &nbsp; &nbsp; 'Anthony' => ['Mr Gates','Mr Musk','Mr Jobs', 'Geography'],&nbsp; &nbsp; &nbsp; &nbsp; 'Sarah' => ['Mr Gates','Mr Jobs','Mr Musk', 'Design'],&nbsp; &nbsp; &nbsp; &nbsp; 'Nelly' => ['Mr Gates','Mr Musk','Mr Jobs', 'Usability'],&nbsp; &nbsp; &nbsp; &nbsp; 'Connor' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics'],&nbsp; &nbsp; &nbsp; &nbsp; 'Frodo' => ['Mr Gates','Mr Musk','Mr Jobs', 'Computer Science'],&nbsp; &nbsp; &nbsp; &nbsp; 'Achmed' => ['Mr Gates','Mr Jobs','Mr Musk', 'Computer Science']&nbsp; &nbsp; ]];$teachers = [&nbsp; &nbsp; 'Mr Jobs' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => []&nbsp; &nbsp; ],&nbsp; &nbsp; 'Mr Gates' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => []&nbsp; &nbsp; ],&nbsp; &nbsp; 'Mr Musk' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => []&nbsp; &nbsp; ],&nbsp; &nbsp; 'Mr Bezos' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => []&nbsp; &nbsp; ]];$unassigned = [];$randomStudentsArray = shuffle_assoc($preferencesOfStudents['students']);/*assign students to prefered spots randomly*/foreach($randomStudentsArray as $name => $student){&nbsp; &nbsp; foreach ($teachers as $teacherName => $array) {&nbsp; &nbsp; &nbsp; &nbsp; if (in_array($student[3],$array['specializations'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $teachers[$teacherName]['students'][] = $name." (because of specialization) (".$student[3].")";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $teachers[$teacherName]['specializations'][] = $student[3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(count($teachers[$student[0]]['students']) < 5 && count($teachers[$student[0]]['specializations']) < 2){&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[0]]['students'][] = $name." (first choice) (".$student[3].")";&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[0]]['specializations'][] = $student[3];&nbsp;&nbsp; &nbsp; } elseif(count($teachers[$student[1]]['students']) < 5 && count($teachers[$student[1]]['specializations']) < 2) {&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[1]]['students'][] = $name." (second choice) (".$student[3].")";&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[1]]['specializations'][] = $student[3];&nbsp; &nbsp; } elseif(count($teachers[$student[2]]['students']) < 5 && count($teachers[$student[2]]['specializations']) < 2)&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[2]]['students'][] = $name." (third choice) (".$student[3].")";&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[2]]['specializations'][] = $student[3];&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $unassigned[$name] = $student;&nbsp; &nbsp; }}/*assign unassigned student to free spots*/foreach($unassigned as $name => $student){&nbsp; &nbsp; foreach($teachers as $teacherName => $listOfStudents){&nbsp; &nbsp; &nbsp; &nbsp; if(count($listOfStudents['students']) < 5 && count($listOfStudents['specializations']) < 2){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $teachers[$teacherName]['students'][] = $name." (none of prefered choices) (".$student[3].")";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $teachers[$teacherName]['specializations'][] = $student[3];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}print_r($teachers);编辑2:<?phpfunction shuffle_assoc($list) {&nbsp; &nbsp; if (!is_array($list)) return $list;&nbsp; &nbsp; $keys = array_keys($list);&nbsp; &nbsp; shuffle($keys);&nbsp; &nbsp; $random = array();&nbsp; &nbsp; foreach ($keys as $key) {&nbsp; &nbsp; &nbsp; &nbsp; $random[$key] = $list[$key];&nbsp; &nbsp; }&nbsp; &nbsp; return $random;}function searchForId($id, $array) {&nbsp; &nbsp; foreach ($array as $key => $val) {&nbsp; &nbsp; &nbsp; &nbsp; if ($val['specializations'] === $id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $key;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return null;&nbsp;}$preferencesOfStudents = [&nbsp; &nbsp; 'students' => [&nbsp; &nbsp; &nbsp; &nbsp; 'Stacy' => ['Mr Jobs','Mr Gates','Mr Musk', 'Biology'],&nbsp; &nbsp; &nbsp; &nbsp; 'Carl' => ['Mr Jobs','Mr Gates','Mr Musk', 'Design'],&nbsp; &nbsp; &nbsp; &nbsp; 'Melody' => ['Mr Bezos','Mr Musk','Mr Gates', 'Usability'],&nbsp; &nbsp; &nbsp; &nbsp; 'William' => ['Mr Musk','Mr Gates','Mr Jobs', 'Computer Science'],&nbsp; &nbsp; &nbsp; &nbsp; 'Eric' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics'],&nbsp; &nbsp; &nbsp; &nbsp; 'Charlott' => ['Mr Jobs','Mr Gates','Mr Musk', 'Geography'],&nbsp; &nbsp; &nbsp; &nbsp; 'Anthony' => ['Mr Gates','Mr Musk','Mr Jobs', 'Geography'],&nbsp; &nbsp; &nbsp; &nbsp; 'Sarah' => ['Mr Gates','Mr Jobs','Mr Musk', 'Design'],&nbsp; &nbsp; &nbsp; &nbsp; 'Nelly' => ['Mr Gates','Mr Musk','Mr Jobs', 'Usability'],&nbsp; &nbsp; &nbsp; &nbsp; 'Connor' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics'],&nbsp; &nbsp; &nbsp; &nbsp; 'Frodo' => ['Mr Gates','Mr Musk','Mr Jobs', 'Computer Science'],&nbsp; &nbsp; &nbsp; &nbsp; 'Achmed' => ['Mr Gates','Mr Jobs','Mr Musk', 'Computer Science']&nbsp; &nbsp; ]];$teachers = [&nbsp; &nbsp; 'Mr Jobs' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => ['Biology', 'Design']&nbsp; &nbsp; ],&nbsp; &nbsp; 'Mr Gates' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => ['Computer Science', 'Usability']&nbsp; &nbsp; ],&nbsp; &nbsp; 'Mr Musk' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => ['Physics', 'Geography']&nbsp; &nbsp; ],&nbsp; &nbsp; 'Mr Bezos' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => ['Usability', 'Biology']&nbsp; &nbsp; ]];$unassigned = [];$randomStudentsArray = shuffle_assoc($preferencesOfStudents['students']);/*assign students to prefered spots randomly*/foreach($randomStudentsArray as $name => $student){&nbsp; &nbsp; if(count($teachers[$student[0]]['students']) < 5 && in_array($student[3],$teachers[$student[0]]['specializations'])){&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[0]]['students'][] = $name." (first choice) (".$student[3].")";&nbsp; &nbsp; } elseif(count($teachers[$student[1]]['students']) < 5 && in_array($student[3],$teachers[$student[1]]['specializations'])) {&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[1]]['students'][] = $name." (second choice) (".$student[3].")";&nbsp; &nbsp; } elseif(count($teachers[$student[2]]['students']) < 5 && in_array($student[3],$teachers[$student[2]]['specializations']))&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[2]]['students'][] = $name." (third choice) (".$student[3].")";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $unassigned[$name] = $student;&nbsp; &nbsp; }}/*assign unassigned student to free spots*/foreach($unassigned as $name => $student){&nbsp; &nbsp; foreach($teachers as $teacherName => $listOfStudents){&nbsp; &nbsp; &nbsp; &nbsp; if(count($listOfStudents['students']) < 5 && count($listOfStudents['specializations']) < 2 && in_array($student[3],$teachers[$teacherName]['specializations'])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $teachers[$teacherName]['students'][] = $name." (none of prefered choices) (".$student[3].")";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}print_r($teachers);编辑3:<?phpfunction shuffle_assoc($list) {&nbsp; &nbsp; if (!is_array($list)) return $list;&nbsp; &nbsp; $keys = array_keys($list);&nbsp; &nbsp; shuffle($keys);&nbsp; &nbsp; $random = array();&nbsp; &nbsp; foreach ($keys as $key) {&nbsp; &nbsp; &nbsp; &nbsp; $random[$key] = $list[$key];&nbsp; &nbsp; }&nbsp; &nbsp; return $random;}function searchForId($id, $array) {&nbsp; &nbsp; foreach ($array as $key => $val) {&nbsp; &nbsp; &nbsp; &nbsp; if ($val['specializations'] === $id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $key;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return null;&nbsp;}$preferencesOfStudents = [&nbsp; &nbsp; 'students' => [&nbsp; &nbsp; &nbsp; &nbsp; 'Stacy' => ['Mr Jobs','Mr Gates','Mr Musk', 'Biology', 'Usability'],&nbsp; &nbsp; &nbsp; &nbsp; 'Carl' => ['Mr Jobs','Mr Gates','Mr Musk', 'Design', 'Physics'],&nbsp; &nbsp; &nbsp; &nbsp; 'Melody' => ['Mr Bezos','Mr Musk','Mr Gates', 'Usability', 'Physics'],&nbsp; &nbsp; &nbsp; &nbsp; 'William' => ['Mr Musk','Mr Gates','Mr Jobs', 'Computer Science', 'Usability'],&nbsp; &nbsp; &nbsp; &nbsp; 'Eric' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics', 'Design'],&nbsp; &nbsp; &nbsp; &nbsp; 'Charlott' => ['Mr Bezos','Mr Gates','Mr Musk', 'Geography', 'Design'],&nbsp; &nbsp; &nbsp; &nbsp; 'Anthony' => ['Mr Gates','Mr Musk','Mr Jobs', 'Geography', 'Computer Science'],&nbsp; &nbsp; &nbsp; &nbsp; 'Sarah' => ['Mr Bezos','Mr Jobs','Mr Musk', 'Design', 'Physics'],&nbsp; &nbsp; &nbsp; &nbsp; 'Nelly' => ['Mr Gates','Mr Musk','Mr Jobs', 'Usability', 'Design'],&nbsp; &nbsp; &nbsp; &nbsp; 'Connor' => ['Mr Gates','Mr Bezos','Mr Musk', 'Physics', 'Usability'],&nbsp; &nbsp; &nbsp; &nbsp; 'Frodo' => ['Mr Gates','Mr Musk','Mr Jobs', 'Computer Science', 'Geography'],&nbsp; &nbsp; &nbsp; &nbsp; 'Achmed' => ['Mr Gates','Mr Jobs','Mr Musk', 'Computer Science', 'Geography'],&nbsp; &nbsp; &nbsp; &nbsp; 'Charlie' => ['Mr Bezos','Mr Musk','Mr Gates', 'Usability', 'Physics'],&nbsp; &nbsp; &nbsp; &nbsp; 'India' => ['Mr Musk','Mr Gates','Mr Jobs', 'Computer Science', 'Usability'],&nbsp; &nbsp; &nbsp; &nbsp; 'Lima' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics', 'Design'],&nbsp; &nbsp; &nbsp; &nbsp; 'Mike' => ['Mr Jobs','Mr Gates','Mr Musk', 'Geography', 'Design'],&nbsp; &nbsp; &nbsp; &nbsp; 'Oscar' => ['Mr Gates','Mr Musk','Mr Jobs', 'Usability', 'Computer Science'],&nbsp; &nbsp; &nbsp; &nbsp; 'Quentin' => ['Mr Gates','Mr Jobs','Mr Musk', 'Design', 'Physics'],&nbsp; &nbsp; &nbsp; &nbsp; 'Sam' => ['Mr Gates','Mr Musk','Mr Jobs', 'Usability', 'Design'],&nbsp; &nbsp; &nbsp; &nbsp; 'Victor' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics', 'Usability'],&nbsp; &nbsp; &nbsp; &nbsp; 'Zach' => ['Mr Gates','Mr Musk','Mr Jobs', 'Computer Science', 'Geography'],&nbsp; &nbsp; &nbsp; &nbsp; 'Arya' => ['Mr Gates','Mr Jobs','Mr Musk', 'Computer Science', 'Geography']&nbsp; &nbsp; ]];$teachers = [&nbsp; &nbsp; 'Mr Jobs' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => ['Biology', 'Design']&nbsp; &nbsp; ],&nbsp; &nbsp; 'Mr Gates' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => ['Computer Science', 'Usability']&nbsp; &nbsp; ],&nbsp; &nbsp; 'Mr Musk' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => ['Physics', 'Geography']&nbsp; &nbsp; ],&nbsp; &nbsp; 'Mr Bezos' => [&nbsp; &nbsp; &nbsp; &nbsp; 'students' => [],&nbsp; &nbsp; &nbsp; &nbsp; 'specializations' => ['Usability', 'Biology']&nbsp; &nbsp; ]];$unassigned = [];$randomStudentsArray = shuffle_assoc($preferencesOfStudents['students']);/*assign students to prefered spots randomly*/foreach($randomStudentsArray as $name => $student){&nbsp; &nbsp; if(count($teachers[$student[0]]['students']) < 5 && (in_array($student[3],$teachers[$student[0]]['specializations']) || in_array($student[3],$teachers[$student[0]]['specializations']))){&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[0]]['students'][] = $name." (first choice) (".$student[3].")";&nbsp; &nbsp; } elseif(count($teachers[$student[1]]['students']) < 5 && (in_array($student[3],$teachers[$student[1]]['specializations']) || in_array($student[3],$teachers[$student[0]]['specializations']))) {&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[1]]['students'][] = $name." (second choice) (".$student[3].")";&nbsp; &nbsp; } elseif(count($teachers[$student[2]]['students']) < 5 && (in_array($student[3],$teachers[$student[2]]['specializations']) || in_array($student[3],$teachers[$student[0]]['specializations'])))&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $teachers[$student[2]]['students'][] = $name." (third choice) (".$student[3].")";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $unassigned[$name] = $student;&nbsp; &nbsp; }}/*assign unassigned student to free spots*/foreach($unassigned as $name => $student){&nbsp; &nbsp; foreach($teachers as $teacherName => $listOfStudents){&nbsp; &nbsp; &nbsp; &nbsp; if(count($listOfStudents['students']) < 5 && count($listOfStudents['specializations']) < 2 && ( in_array($student[3],$teachers[$teacherName]['specializations']) || in_array($student[4],$teachers[$teacherName]['specializations']) )){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $teachers[$teacherName]['students'][] = $name." (none of prefered choices) (".$student[3].")";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}print_r($teachers);
打开App,查看更多内容
随时随地看视频慕课网APP