Implemented hogwarts
This commit is contained in:
parent
7d9db66ec7
commit
d8ddcbdd21
1 changed files with 35 additions and 2 deletions
|
@ -2,6 +2,39 @@
|
||||||
|
|
||||||
package hogwarts
|
package hogwarts
|
||||||
|
|
||||||
func GetCourseList(prereqs map[string][]string) []string {
|
import (
|
||||||
return []string{}
|
"log"
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
func singleCourseList(
|
||||||
|
course string, prereqs map[string][]string, list []string, traversed []string,
|
||||||
|
) []string {
|
||||||
|
if slices.Contains(traversed, course) {
|
||||||
|
log.Panic("Recursive prereqs, aborting...")
|
||||||
|
}
|
||||||
|
traversed = append(traversed, course)
|
||||||
|
for _, prereqCourse := range prereqs[course] {
|
||||||
|
if !slices.Contains(list, prereqCourse) {
|
||||||
|
i := slices.Index(list, course)
|
||||||
|
if i == -1 {
|
||||||
|
list = append(list, prereqCourse)
|
||||||
|
} else {
|
||||||
|
list = slices.Insert(list, i, prereqCourse)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list = singleCourseList(prereqCourse, prereqs, list, traversed)
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCourseList(prereqs map[string][]string) []string {
|
||||||
|
list := make([]string, 0)
|
||||||
|
for course := range prereqs {
|
||||||
|
if !slices.Contains(list, course) {
|
||||||
|
list = append(list, course)
|
||||||
|
}
|
||||||
|
list = singleCourseList(course, prereqs, list, []string{})
|
||||||
|
}
|
||||||
|
return list
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue