为了账号安全,请及时绑定邮箱和手机立即绑定

通过附加要求扩展阵列

通过附加要求扩展阵列

PHP
繁星coding 2023-09-15 17:04:54
我是 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);
查看完整描述

1 回答

?
MMTTMM

TA贡献1869条经验 获得超4个赞

编辑:这有效吗?希望能有所帮助!


如果最多有 2 个专业,则会出现一个问题:有时并非所有学生都可以分配。


但它会尽可能地尝试,而且是随机的,所以如果它没有给出好的输出,就再试一次。


   <?php

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;

}

function searchForId($id, $array) {

    foreach ($array as $key => $val) {

        if ($val['specializations'] === $id) {

            return $key;

        }

    }

    return null;

 }

$preferencesOfStudents = [

    'students' => [

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

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

        'Melody' => ['Mr Bezos','Mr Musk','Mr Gates', 'Usability'],

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

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

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

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

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

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

        'Connor' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics'],

        'Frodo' => ['Mr Gates','Mr Musk','Mr Jobs', 'Computer Science'],

        'Achmed' => ['Mr Gates','Mr Jobs','Mr Musk', 'Computer Science']

    ]

];


$teachers = [

    'Mr Jobs' => [

        'students' => [],

        'specializations' => []

    ],

    'Mr Gates' => [

        'students' => [],

        'specializations' => []

    ],

    'Mr Musk' => [

        'students' => [],

        'specializations' => []

    ],

    'Mr Bezos' => [

        'students' => [],

        'specializations' => []

    ]

];

$unassigned = [];

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

/*assign students to prefered spots randomly*/

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

    foreach ($teachers as $teacherName => $array) {

        if (in_array($student[3],$array['specializations'])) {

            $teachers[$teacherName]['students'][] = $name." (because of specialization) (".$student[3].")";

            $teachers[$teacherName]['specializations'][] = $student[3];

            break;

        }

    }

    if(count($teachers[$student[0]]['students']) < 5 && count($teachers[$student[0]]['specializations']) < 2){   

        $teachers[$student[0]]['students'][] = $name." (first choice) (".$student[3].")";

        $teachers[$student[0]]['specializations'][] = $student[3]; 

    } elseif(count($teachers[$student[1]]['students']) < 5 && count($teachers[$student[1]]['specializations']) < 2) {

        $teachers[$student[1]]['students'][] = $name." (second choice) (".$student[3].")";

        $teachers[$student[1]]['specializations'][] = $student[3];

    } elseif(count($teachers[$student[2]]['students']) < 5 && count($teachers[$student[2]]['specializations']) < 2)  {

        $teachers[$student[2]]['students'][] = $name." (third choice) (".$student[3].")";

        $teachers[$student[2]]['specializations'][] = $student[3];

    } else {

        $unassigned[$name] = $student;

    }

}

/*assign unassigned student to free spots*/

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

    foreach($teachers as $teacherName => $listOfStudents){

        if(count($listOfStudents['students']) < 5 && count($listOfStudents['specializations']) < 2){

            $teachers[$teacherName]['students'][] = $name." (none of prefered choices) (".$student[3].")";

            $teachers[$teacherName]['specializations'][] = $student[3];

        }

    }

}

print_r($teachers);

编辑2:


<?php

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;

}

function searchForId($id, $array) {

    foreach ($array as $key => $val) {

        if ($val['specializations'] === $id) {

            return $key;

        }

    }

    return null;

 }

$preferencesOfStudents = [

    'students' => [

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

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

        'Melody' => ['Mr Bezos','Mr Musk','Mr Gates', 'Usability'],

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

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

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

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

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

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

        'Connor' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics'],

        'Frodo' => ['Mr Gates','Mr Musk','Mr Jobs', 'Computer Science'],

        'Achmed' => ['Mr Gates','Mr Jobs','Mr Musk', 'Computer Science']

    ]

];


$teachers = [

    'Mr Jobs' => [

        'students' => [],

        'specializations' => ['Biology', 'Design']

    ],

    'Mr Gates' => [

        'students' => [],

        'specializations' => ['Computer Science', 'Usability']

    ],

    'Mr Musk' => [

        'students' => [],

        'specializations' => ['Physics', 'Geography']

    ],

    'Mr Bezos' => [

        'students' => [],

        'specializations' => ['Usability', 'Biology']

    ]

];

$unassigned = [];

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

/*assign students to prefered spots randomly*/

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

    if(count($teachers[$student[0]]['students']) < 5 && in_array($student[3],$teachers[$student[0]]['specializations'])){   

        $teachers[$student[0]]['students'][] = $name." (first choice) (".$student[3].")";

    } elseif(count($teachers[$student[1]]['students']) < 5 && in_array($student[3],$teachers[$student[1]]['specializations'])) {

        $teachers[$student[1]]['students'][] = $name." (second choice) (".$student[3].")";

    } elseif(count($teachers[$student[2]]['students']) < 5 && in_array($student[3],$teachers[$student[2]]['specializations']))  {

        $teachers[$student[2]]['students'][] = $name." (third choice) (".$student[3].")";

    } else {

        $unassigned[$name] = $student;

    }

}

/*assign unassigned student to free spots*/

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

    foreach($teachers as $teacherName => $listOfStudents){

        if(count($listOfStudents['students']) < 5 && count($listOfStudents['specializations']) < 2 && in_array($student[3],$teachers[$teacherName]['specializations'])){

            $teachers[$teacherName]['students'][] = $name." (none of prefered choices) (".$student[3].")";

        }

    }

}

print_r($teachers);

编辑3:


<?php

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;

}

function searchForId($id, $array) {

    foreach ($array as $key => $val) {

        if ($val['specializations'] === $id) {

            return $key;

        }

    }

    return null;

 }

$preferencesOfStudents = [

    'students' => [

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

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

        'Melody' => ['Mr Bezos','Mr Musk','Mr Gates', 'Usability', 'Physics'],

        'William' => ['Mr Musk','Mr Gates','Mr Jobs', 'Computer Science', 'Usability'],

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

        'Charlott' => ['Mr Bezos','Mr Gates','Mr Musk', 'Geography', 'Design'],

        'Anthony' => ['Mr Gates','Mr Musk','Mr Jobs', 'Geography', 'Computer Science'],

        'Sarah' => ['Mr Bezos','Mr Jobs','Mr Musk', 'Design', 'Physics'],

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

        'Connor' => ['Mr Gates','Mr Bezos','Mr Musk', 'Physics', 'Usability'],

        'Frodo' => ['Mr Gates','Mr Musk','Mr Jobs', 'Computer Science', 'Geography'],

        'Achmed' => ['Mr Gates','Mr Jobs','Mr Musk', 'Computer Science', 'Geography'],

        'Charlie' => ['Mr Bezos','Mr Musk','Mr Gates', 'Usability', 'Physics'],

        'India' => ['Mr Musk','Mr Gates','Mr Jobs', 'Computer Science', 'Usability'],

        'Lima' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics', 'Design'],

        'Mike' => ['Mr Jobs','Mr Gates','Mr Musk', 'Geography', 'Design'],

        'Oscar' => ['Mr Gates','Mr Musk','Mr Jobs', 'Usability', 'Computer Science'],

        'Quentin' => ['Mr Gates','Mr Jobs','Mr Musk', 'Design', 'Physics'],

        'Sam' => ['Mr Gates','Mr Musk','Mr Jobs', 'Usability', 'Design'],

        'Victor' => ['Mr Gates','Mr Jobs','Mr Musk', 'Physics', 'Usability'],

        'Zach' => ['Mr Gates','Mr Musk','Mr Jobs', 'Computer Science', 'Geography'],

        'Arya' => ['Mr Gates','Mr Jobs','Mr Musk', 'Computer Science', 'Geography']

    ]

];


$teachers = [

    'Mr Jobs' => [

        'students' => [],

        'specializations' => ['Biology', 'Design']

    ],

    'Mr Gates' => [

        'students' => [],

        'specializations' => ['Computer Science', 'Usability']

    ],

    'Mr Musk' => [

        'students' => [],

        'specializations' => ['Physics', 'Geography']

    ],

    'Mr Bezos' => [

        'students' => [],

        'specializations' => ['Usability', 'Biology']

    ]

];

$unassigned = [];

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

/*assign students to prefered spots randomly*/

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

    if(count($teachers[$student[0]]['students']) < 5 && (in_array($student[3],$teachers[$student[0]]['specializations']) || in_array($student[3],$teachers[$student[0]]['specializations']))){   

        $teachers[$student[0]]['students'][] = $name." (first choice) (".$student[3].")";

    } elseif(count($teachers[$student[1]]['students']) < 5 && (in_array($student[3],$teachers[$student[1]]['specializations']) || in_array($student[3],$teachers[$student[0]]['specializations']))) {

        $teachers[$student[1]]['students'][] = $name." (second choice) (".$student[3].")";

    } elseif(count($teachers[$student[2]]['students']) < 5 && (in_array($student[3],$teachers[$student[2]]['specializations']) || in_array($student[3],$teachers[$student[0]]['specializations'])))  {

        $teachers[$student[2]]['students'][] = $name." (third choice) (".$student[3].")";

    } else {

        $unassigned[$name] = $student;

    }

}

/*assign unassigned student to free spots*/

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

    foreach($teachers as $teacherName => $listOfStudents){

        if(count($listOfStudents['students']) < 5 && count($listOfStudents['specializations']) < 2 && ( in_array($student[3],$teachers[$teacherName]['specializations']) || in_array($student[4],$teachers[$teacherName]['specializations']) )){

            $teachers[$teacherName]['students'][] = $name." (none of prefered choices) (".$student[3].")";

        }

    }

}

print_r($teachers);



查看完整回答
反对 回复 2023-09-15
  • 1 回答
  • 0 关注
  • 102 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信