Created
May 7, 2013 18:58
-
-
Save bionoren/5535153 to your computer and use it in GitHub Desktop.
All the course combinations. Well, all the ones the might pan out.
This file contains 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
/** | |
* Creates a list of classes that can be taken (i.e. do not cause conflicts with other classes and can be used | |
* in a valid schedule.) from the given courses. | |
* | |
* Note that this function (the while loop in particular) is a significant performance bottleneck | |
* | |
* @param ARRAY $courses List of course objects to consider. | |
* @return MIXED A list of valid classes or a string with the error message(s). | |
*/ | |
function findSchedules(array $courses) { | |
usort($courses, function($section1, $section2) { | |
return count($section1) - count($section2); | |
}); | |
$numCourses = count($courses); | |
$indexes = array_fill(0, $numCourses, 0); | |
$courseCounts = array(); | |
foreach($courses as $arr) { | |
$courseCounts[] = count($arr); | |
} | |
$classes = array(); | |
for($i = 0; $i < $numCourses; $i++) { | |
$classes[$i] = $courses[$i][0]; | |
} | |
while(true) { | |
if(isValidSchedule($classes)) { | |
foreach($classes as $class) { | |
$class->conflict = null; | |
$class->valid = true; | |
} | |
} | |
//for each course, if the index for this course is less than the max section index, shift it | |
//also handles rollover for previous indicies, and updates the currently evaluated classes array | |
for($i = 0, $classes[0] = @$courses[0][++$indexes[0]]; $indexes[$i] == $courseCounts[$i];) { | |
$classes[$i] = $courses[$i][0]; | |
$indexes[$i++] = 0; | |
//this exits the loop | |
if($i == $numCourses) break 2; | |
@$classes[$i] = $courses[$i][++$indexes[$i]]; | |
} | |
} | |
$conflict = findConflicts($courses); | |
if(!empty($conflict)) { | |
return implode("<br>", $conflict); | |
} | |
return $courses; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment